In this article, we will discuss how to get current date and time using the legacy Date and Calendar apis alongwith the Java 8 LocalDate, LocalTime, LocalDateTime and ZonedDateTime apis.
Current date using java.util.Date instance
The Date class constructor creates a Date object that represents the time at which it was allocated.
Date d1 = new Date(); System.out.println(d1); // Tue Mar 06 22:50:37 EST 2018
If you are looking to format the current date in a specific format, use the SimpleDateFormat class:
Date d1 = new Date(); DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); System.out.println(sdf.format(d1)); // 06/03/2018 22:50:37
If you are looking for current date without time, modify the pattern accordingly :
Date d1 = new Date(); DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); System.out.println(sdf.format(d1)); // 03/06/2018
System.currentTimeMillis()
This System class method returns the current time in milliseconds.
System.out.println(System.currentTimeMillis()); // 1520398221562
Current date using java.util.Calendar instance
Calendar c1 = Calendar.getInstance(); System.out.println(c1.getTime()); // Tue Mar 06 22:50:37 EST 2018
Again, to get the current date in a particular format, use the SimpleDateFormat class:
Calendar c1 = Calendar.getInstance();DateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); System.out.println(sdf1.format(c1.getTime())); // 06/03/2018 22:50:37
Current date using Java 8 Date Time api
Java 8 introduced a new java.time package which contains LocalDate, LocalTime and LocalDateTime classes.
These classes can not be instantiated with new operator.
To get the current Date and Time, we can use the static now() method of LocalDate and LocalTime classes.
LocalDate date = LocalDate.now(); System.out.println(date); // 2018-03-06 LocalTime time = LocalTime.now(); System.out.println(time); // 22:50:37.669 LocalDateTime dateTime = LocalDateTime.now(); System.out.println(dateTime); // 2018-03-06T22:50:37.670
To format the LocalDate, use the DateTimeFormatter class :
LocalDate date = LocalDate.now(); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy"); System.out.println(dtf.format(date)); // 03/06/2018
Similarly for or LocalDateTime :
LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter dateTimeformatter = DateTimeFormatter.ofPattern("MMMM dd yyyy hh':'mm a"); System.out.println(dateTimeformatter.format(dateTime)); // March 06 2018 10:50 PM
We can also use the ZonedDateTime class to obtain the current date with timezone info:
ZonedDateTime z1 = ZonedDateTime.now(); System.out.println(z1); // 2018-03-06T22:50:37.796-05:00[America/New_York]
Here is the complete program for reference :
package com.topjavatutorial; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; public class Hello { public static void main(String[] args) { Date d1 = new Date(); System.out.println(d1); DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); System.out.println(sdf.format(d1)); Calendar c1 = Calendar.getInstance(); System.out.println(c1.getTime()); DateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); System.out.println(sdf1.format(c1.getTime())); LocalDate date = LocalDate.now(); System.out.println(date); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy"); System.out.println(dtf.format(date)); LocalTime time = LocalTime.now(); System.out.println(time); LocalDateTime dateTime = LocalDateTime.now(); System.out.println(dateTime); DateTimeFormatter dateTimeformatter = DateTimeFormatter.ofPattern("MMMM dd yyyy hh':'mm a"); System.out.println(dateTimeformatter.format(dateTime)); ZonedDateTime z1 = ZonedDateTime.now(); System.out.println(z1); } }
© 2018 – 2023, https:. All rights reserved. On republishing this post, you must provide link to original post