September 24, 2022

Getting Current Date and Time in Java

In this post we’ll see different ways to get current date and time in Java. Options you have are-

  1. java.util.Date
  2. java.util.Calendar
  3. java.time.LocalDate- To get date.
  4. java.time.LocalTime- To get time.
  5. java.time.LocalDateTime- To get both date and time.
  6. java.time.ZonedDateTime – If you want time-zone information too.

Out of these classes LocalDate, LocalTime, LocalDateTime and ZonedDateTime are classes in Java 8 new Date and Time API.

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

When you instantiate a Date object, it is initialized so that it represents the date and time at which it was allocated.

Date date = new Date();
System.out.println(date);
Output
Thu Oct 10 16:42:21 IST 2019
Using SimpleDateFormat you can format this date.
public class FormatDate {
  public static void main(String[] args) {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
    System.out.println(sdf.format(date));
  }
}
Output
10/10/2019 04:50:49.197

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

Using getInstance() static method of the Calendar class you can get an instance of Calendar.

public class FormatDate {
  public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy hh:mm:ss");
    System.out.println(sdf.format(calendar.getTime()));
  }
}

3. Using java.time.LocalDate

LocalDate represents a date without time-zone in the ISO-8601 calendar system. Using now() method you can obtain the current date from the system clock in the default time-zone.

For formatting date you can use DateTimeFormatter class which is also added in Java 8.

public class FormatDate {
  public static void main(String[] args) {
    // get date
    LocalDate date = LocalDate.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    System.out.println(date.format(formatter));
  }
}
Output
10/10/2019

4. Using java.time.LocalTime

LocalTime represents a time without a time-zone in the ISO-8601 calendar system, such as 08:10:30.

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

public class FormatDate {
  public static void main(String[] args) {
    // get time
    LocalTime date = LocalTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
    System.out.println(date.format(formatter));
  }
}
Output
05:11:31 PM

5. Using java.time.LocalDateTime

LocalDateTime represents a date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.

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

public class FormatDate {
  public static void main(String[] args) {
    // get datetime
    LocalDateTime dateTime = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
    System.out.println(dateTime.format(formatter));
  }
}
Output
2019-10-10T17:14:41.098

6. Using java.time.ZonedDateTime

ZonedDateTime represents date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris. If you want zone offset and time-zone then you can use ZonedDateTime instance.

public class FormatDate {
  public static void main(String[] args) {
    // get datetime
    ZonedDateTime dateTime = ZonedDateTime.now();
    //z=time-zone name, V=time-zone ID
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS z VV");
    System.out.println(dateTime.format(formatter));
  }
}
Output
2019-10-10T17:22:31.958 IST Asia/Calcutta

That's all for the topic Getting Current Date and Time in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment