Learn to get the current date and/or time in Java. Note that the appropriate way to handle date-time information is different before and after JDK 8.
- For JDK 8 or later, the recommended way is to use
LocalDate
andLocalTime
classes. - For JDK 7 or earlier, we can use of
Date
andCalendar
classes only.
1. Get Current Date and Time (Java 8 or Later)
1.1. Core Classes
In Java 8 or later, the date and time information is represented by the following classes. These classes provide the current date and time locally to the user, and there is no timezone information is associated with it.
- java.time.LocalDate – Represents the Date only information in yyyy-MM-dd pattern.
- java.time.LocalTime – Represents the Time only information in HH:mm:ss.SSSSSSSSS pattern.
- java.time.LocalDateTime – Represents the Date and Time informations, both, without any timezone information. The pattern is the combination of local date and time information.
To get the current date and time information in another timezone/locale, we can use the following classes.
- java.time.ZonedDateTime – Represents the date and time information in a given timezone.
1.2. Code Examples
The following code shows how to get the current date-time information using the now()
method in each class. The now() method returns an immutable and thread-safe instance of the class for which it is invoked.
// Get current date
LocalDate today = LocalDate.now();
System.out.println(today); //2022-02-15
// Get current time
LocalTime now = LocalTime.now();
System.out.println(now); //12:43:51.519251200
// Get current date and time
LocalDateTime instance = LocalDateTime.now();
System.out.println(instance); //2022-02-15T12:43:51.519251200
To get the timezone-specific date and time information, pass the zone information in the ZonedDateTime.now() method.
// Get current date and time in GMT
ZonedDateTime tzInstance = ZonedDateTime.now(ZoneId.of("GMT"));
System.out.println(tzInstance); //2022-02-15T07:13:51.519251200Z[GMT]
1.3. Display Formatted Date and Time
To default string representations of the above classes are fixed. If we want to display the information in some custom pattern, we can use DateTimeFormatter
class.
LocalDateTime instance = LocalDateTime.now();
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm");
String formattedString = formatter.format(instance); //15-02-2022 12:43
2. Get Current Date and Time (Java 7 or Earlier)
2.1. Core Classes
Till version 7 or earlier, Java didn’t have separate classes for the date and time parts. The main classes were :
java.util.Date
java.util.Calendar
2.2. Code Examples
Let us have a quick look at the methods used for getting the current date and time information in legacy Java classes.
Date date = new Date();
System.out.println(date); //Tue Feb 15 13:00:31 IST 2022
Calendar cal = Calendar.getInstance();
System.out.println(cal);
2.3. Display Formatted Date and Time
To display, the date-time in a custom formatted manner, we should use SimpleDateFormat
class.
Date date = new Date();
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm");
System.out.println(sdf.format(date)); //15-02-2022 01:04
System.out.println(sdf.format(cal.getTime())); //15-02-2022 01:04
Happy Learning !!