This post will discuss how to get the current date and time in Java.

1. Using LocalDateTime class

From Java 8 onwards, we can use the java.time package that offers a lot of improvement over the existing Java date and time APIs. To get the current date-time using the system clock and default time-zone, we can use the LocalDateTime.now() method. The java.time.LocalDateTime class represents a date and time without a time zone. It also provides various methods access the components (year, month, day, hour, minute, second, etc.) individually such as getYear(), getMonth(), getDayOfWeek(), getHour(), getMinute(), getSecond(), etc. Here’s an example:

Download  Run Code

 
To get the current date and time as a formatted string, we can use the DateTimeFormatter class from the java.time.format package. It provides predefined and custom patterns for parsing and formatting date and time. We can pass the formatter to the format() method of the LocalDateTime class. Here’s an example:

Download  Run Code

 
We can also create a custom date-time formatter over the predefined date-time formatters using the DateTimeFormatter.ofPattern() method. Here’s an example:

Download  Run Code

2. Using ZonedDateTime class

To get the time-zone information from the system clock, consider switching to the java.time.ZonedDateTime class. It provides similar methods as the java.time.LocalDateTime class in addition to the time zone information. To obtain the current date-time from the system clock in the specified time-zone, we can specify a Zone ID to the ZonedDateTime.now() method. To format the date-time, we can specify a formatter using the format() method. Here’s an example:

Download  Run Code

3. Using Date class

Before Java 8, the standard solution to get current date and time in Java is using the java.util.Date object. To initialize a Date object with the current date and time with millisecond precision, we can call its constructor without any arguments. The Date class is part of the legacy Java Date and Time API. Here’s an example:

Download  Run Code

 
To get the current date and time in the specified format, we can use the java.text.SimpleDateFormat class. It allows us to format and parse dates with predefined and user-defined patterns for date-time formatting. Here’s an example:

Download  Run Code

4. Joda Time

The standard date and time classes before Java 8 are poorly designed and contain several flaws. The Joda-Time date-time library provides a high-quality replacement prior to Java 8. To get the full date and time with time-zone, we can use the DateTime class. The Joda-Time class also supports the time zone functionality through the DateTimeZone class. Here’s an example:

Download Code

5. Using Instant class

The Instant class models an instantaneous point on the timeline, independent of any time zone or calendar. To obtain the current instant using the system clock, we can use the Instant.now() method. Here’s an example:

Download  Run Code

 
To obtain a Date instance from an Instant object, we can call the Date.from() method. Here’s an example:

Download  Run Code

That’s all about getting the current date and time in Java.

 
Related Posts:

Get date without time in Java

Get time without date in Java