Monday, January 15, 2024

Java Program to Get Current Date and Time

This article shows how to get current date and time in Java. In the new data and time API from Java 8 onward there are following classes that can be used for displaying either current date, current time or both.

  • java.time.LocalDate- To get current date.
  • java.time.LocalTime- To get current time.
  • java.time.LocalDateTime- To get both current date and time.
  • java.time.ZonedDateTime– To get both current date and time with time-zone information.

Other options, mostly applicable, if you are still not using Java 8 version or above are as follows.

  • By using java.util.Date class
  • By using java.util.Calendar class

1. Current date using LocalDate in Java

Using now() method of the LocalDate you can obtain the current date from the system clock in the default time-zone. For formatting the date in required format you can use DateTimeFormatter class also added in Java 8.

LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(currentDate.format(formatter)); //2020-06-22

2. Current time using LocalTime in Java

Using now() method of the LocalTime class you can obtain the current time from the system clock in the default time-zone.

LocalTime currentTime = LocalTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss");
System.out.println(currentTime.format(formatter)); //09:54:09

3. Current date and time using LocalDateTime in Java

Using now() method of LocalDateTime class you can get the current date-time from the system clock in the default time-zone.

LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(currentDateTime.format(formatter)); //2020-06-22T09:58:46

4. Current date and time with zone information

By using ZonedDateTime you can get zone offset and time-zone information too.

ZonedDateTime currentDateTime = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss z VV");
System.out.println(currentDateTime.format(formatter)); //2020-06-21T21:32:22 GMT-07:00 America/Los_Angeles

5. Getting Date and Time using java.util.Date

When a new Date object is created it is initialized to represent the time at which it was allocated.

Date date = new Date();
System.out.println("Date- " + date); // Date- Mon Jun 22 10:06:12 IST 2020

For formatting date you can use SimpleDateFormat class.

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("Date- " + sdf.format(date)); // 2020-06-22 10:07:38

6. Getting Date and Time using java.util.Calendar

In Calendar class there is a getInstance() method that gives the current date and time.

Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("Date- " + sdf.format(calendar.getTime())); // 2020-06-22 10:10:34

That's all for this topic Java Program to Get Current Date and Time. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Create Date Object in Java With Date and Time Values
  2. How to Convert String to Date in Java
  3. How to Convert Date And Time Between Different Time-Zones in Java
  4. How to Compile Java Program at Runtime
  5. Converting double to int in Java

You may also like-

  1. Converting String to Enum Type in Java
  2. Invoking Getters And Setters Using Reflection in Java
  3. Connection Pooling Using C3P0 in Java
  4. final Vs finally Vs finalize in Java
  5. Map Operation in Java Stream API
  6. Spring MVC Redirect Example
  7. Spring Boot StandAlone (Console Based) Application Example
  8. Angular ngFor Directive With Examples