Java – Date class

Java – Date class 2017-08-09T12:22:47+00:00

Java Date class :

The Date classes encapsulate information about a specific date and time.

It provides the following constructors.

Date()

Date(long msec)

  • Here, the first form returns an object that represent the current date and time.
  • The second form returns an object that represents the date and time msec in milliseconds after the time.

The time is defined as midnight on January 1,1970 GMT (Greenwich Mean Time).

 

Method Description
Boolean after(Date d) Returns true if d is after the current date. Otherwise, returns false.
Boolean before(Date d) Returns true if d is before the current date. Otherwise, returns false.
Boolean equals(Date d) Returns true if d has the same value as the current date. Otherwise, returns false.
Long getTime() Returns the number of milliseconds since the epoch.
Void setTime

(long msec)

Sets the date and time of the current object to represent msec milliseconds since the epoch.
String toString() Returns the string equivalent of the date.

EX :


 


Output :
Fri May 25 00:04:06 IST 2012
Thu Jan 01 05:30:00 IST 1970
EX :

 

Output :
First Date : Fri May 25 00:06:46 IST 2012
Second Date : Fri May 25 00:06:47 IST 2012
Is second date after first ? : true
EX :

 


Output :
Date is : Fri May 25 00:12:52 IST 2012
Milliseconds since January 1, 1970, 00:00:00 GMT : 1337884972842
Time after 10 second Thu Jan 01 05:30:00 IST 1970
Time after 10 second Thu Jan 01 05:30:10 IST 1970
Thu Jan 01 05:30:10 IST 1970

Prev Next