Java - Date and Time
Java - Date and Time
Java - Date and Time
Java provides the Date class available in java.util package, this class encapsulates the current
date and time.
The Date class supports two constructors as shown in the following table.
1 Date( )
This constructor initializes the object with the current date and time.
2 Date(long millisec)
This constructor accepts an argument that equals the number of milliseconds that
have elapsed since midnight, January 1, 1970.
https://www.tutorialspoint.com/java/java_date_time.htm 1/18
15/04/2022, 16:16 Java - Date and Time
1
boolean after(Date date)
Returns true if the invoking Date object contains a date that is later than the one
specified by date, otherwise, it returns false.
2
boolean before(Date date)
Returns true if the invoking Date object contains a date that is earlier than the one
specified by date, otherwise, it returns false.
3
Object clone( )
Duplicates the invoking Date object.
Compares the value of the invoking object with that of date. Returns 0 if the values
are equal. Returns a negative value if the invoking object is earlier than date.
Returns a positive value if the invoking object is later than date.
5
int compareTo(Object obj)
Operates identically to compareTo(Date) if obj is of class Date. Otherwise, it throws a
ClassCastException.
6
boolean equals(Object date)
Returns true if the invoking Date object contains the same time and date as the one
specified by date, otherwise, it returns false.
7 long getTime( )
Returns the number of milliseconds that have elapsed since January 1, 1970.
8 int hashCode( )
9
void setTime(long time)
Sets the time and date as specified by time, which represents an elapsed time in
milliseconds from midnight, January 1, 1970.
10
https://www.tutorialspoint.com/java/java_date_time.htm 2/18
15/04/2022, 16:16 Java - Date and Time
String toString( )
Converts the invoking Date object into a string and returns the result.
Example
Live Demo
import java.util.Date;
System.out.println(date.toString());
Output
Date Comparison
You can use getTime( ) to obtain the number of milliseconds that have elapsed since
midnight, January 1, 1970, for both objects and then compare these two values.
You can use the methods before( ), after( ), and equals( ). Because the 12th of the
month comes before the 18th, for example, new Date(99, 2, 12).before(new Date (99, 2,
18)) returns true.
You can use the compareTo( ) method, which is defined by the Comparable interface
and implemented by Date.
time formatting.
Example
Live Demo
import java.util.*;
import java.text.*;
SimpleDateFormat ft =
Output
To specify the time format, use a time pattern string. In this pattern, all ASCII letters are reserved
as pattern letters, which are defined as the following −
https://www.tutorialspoint.com/java/java_date_time.htm 4/18
15/04/2022, 16:16 Java - Date and Time
G Era designator AD
d Day in month 10
m Minute in hour 30
s Second in minute 55
S Millisecond 234
w Week in year 40
W Week in month 1
a A.M./P.M. marker PM
Date and time formatting can be done very easily using printf method. You use a two-letter
format, starting with t and ending in one of the letters of the table as shown in the following code.
Example
Live Demo
https://www.tutorialspoint.com/java/java_date_time.htm 5/18
15/04/2022, 16:16 Java - Date and Time
import java.util.Date;
System.out.printf(str);
Output
It would be a bit silly if you had to supply the date multiple times to format each part. For that
reason, a format string can indicate the index of the argument to be formatted.
Example
Live Demo
import java.util.Date;
Output
https://www.tutorialspoint.com/java/java_date_time.htm 6/18
15/04/2022, 16:16 Java - Date and Time
Alternatively, you can use the < flag. It indicates that the same argument as in the preceding
format specification should be used again.
Example
Live Demo
import java.util.Date;
Output
https://www.tutorialspoint.com/java/java_date_time.htm 7/18
15/04/2022, 16:16 Java - Date and Time
https://www.tutorialspoint.com/java/java_date_time.htm 8/18
15/04/2022, 16:16 Java - Date and Time
There are other useful classes related to Date and time. For more details, you can refer to Java
Standard documentation.
Example
Live Demo
import java.util.*;
import java.text.*;
Date t;
try {
t = ft.parse(input);
System.out.println(t);
} catch (ParseException e) {
https://www.tutorialspoint.com/java/java_date_time.htm 9/18
15/04/2022, 16:16 Java - Date and Time
A sample run of the above program would produce the following result −
Output
You can sleep for any period of time from one millisecond up to the lifetime of your computer.
For example, the following program would sleep for 3 seconds −
Example
Live Demo
import java.util.*;
try {
Thread.sleep(5*60*10);
} catch (Exception e) {
System.out.println("Got an exception!");
Output
Sometimes, you may need to measure point in time in milliseconds. So let's re-write the above
example once again −
Example
Live Demo
import java.util.*;
https://www.tutorialspoint.com/java/java_date_time.htm 10/18
15/04/2022, 16:16 Java - Date and Time
try {
Thread.sleep(5*60*10);
} catch (Exception e) {
System.out.println("Got an exception!");
Output
Difference is : 5993
GregorianCalendar Class
GregorianCalendar is a concrete implementation of a Calendar class that implements the normal
Gregorian calendar with which you are familiar. We did not discuss Calendar class in this
tutorial, you can look up standard Java documentation for this.
The getInstance( ) method of Calendar returns a GregorianCalendar initialized with the current
date and time in the default locale and time zone. GregorianCalendar defines two fields: AD and
BC. These represent the two eras defined by the Gregorian calendar.
https://www.tutorialspoint.com/java/java_date_time.htm 11/18
15/04/2022, 16:16 Java - Date and Time
1 GregorianCalendar()
Constructs a default GregorianCalendar using the current time in the default time
zone with the default locale.
3 GregorianCalendar(int year, int month, int date, int hour, int minute)
Constructs a GregorianCalendar with the given date and time set for the default time
zone with the default locale.
4 GregorianCalendar(int year, int month, int date, int hour, int minute, int second)
Constructs a GregorianCalendar with the given date and time set for the default time
zone with the default locale.
5 GregorianCalendar(Locale aLocale)
Constructs a GregorianCalendar based on the current time in the default time zone
with the given locale.
6 GregorianCalendar(TimeZone zone)
Constructs a GregorianCalendar based on the current time in the given time zone
with the default locale.
Here is the list of few useful support methods provided by GregorianCalendar class −
https://www.tutorialspoint.com/java/java_date_time.htm 12/18
15/04/2022, 16:16 Java - Date and Time
8
int getGreatestMinimum(int field)
Returns highest minimum value for the given field if varies.
9 Date getGregorianChange()
Gets the Gregorian Calendar change date.
https://www.tutorialspoint.com/java/java_date_time.htm 13/18
15/04/2022, 16:16 Java - Date and Time
12 Date getTime()
Gets this Calendar's current time.
13 long getTimeInMillis()
14 TimeZone getTimeZone()
Gets the time zone.
16 int hashCode()
Overrides hashCode.
Adds or subtracts (up/down) a single unit of time on the given time field without
changing larger fields.
21 void set(int year, int month, int date, int hour, int minute)
Sets the values for the fields year, month, date, hour, and minute.
22 void set(int year, int month, int date, int hour, int minute, int second)
Sets the values for the fields year, month, date, hour, minute, and second.
https://www.tutorialspoint.com/java/java_date_time.htm 14/18
15/04/2022, 16:16 Java - Date and Time
25
void setTimeInMillis(long millis)
Sets this Calendar's current time from the given long value.
27 String toString()
Returns a string representation of this calendar.
Example
Live Demo
import java.util.*;
String months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "A
"Oct", "Nov", "Dec"};
int year;
System.out.print("Date: ");
System.out.print(months[gcalendar.get(Calendar.MONTH)]);
System.out.println(year = gcalendar.get(Calendar.YEAR));
System.out.print("Time: ");
System.out.print(gcalendar.get(Calendar.HOUR) + ":");
System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
System.out.println(gcalendar.get(Calendar.SECOND));
https://www.tutorialspoint.com/java/java_date_time.htm 15/18
15/04/2022, 16:16 Java - Date and Time
if(gcalendar.isLeapYear(year)) {
}else {
Output
Time: 11:25:27
For a complete list of constant available in Calendar class, you can refer the standard Java
documentation.
16 Lectures 2 hours
Malhar Lathkar
More Detail
Video
19 Lectures 5 hours
https://www.tutorialspoint.com/java/java_date_time.htm 16/18
15/04/2022, 16:16 Java - Date and Time
Malhar Lathkar
More Detail
Video
Anadi Sharma
More Detail
Video
More Detail
Video
More Detail
https://www.tutorialspoint.com/java/java_date_time.htm 17/18
15/04/2022, 16:16 Java - Date and Time
Video
76 Lectures 7 hours
Arnab Chakraborty
More Detail
https://www.tutorialspoint.com/java/java_date_time.htm 18/18