Get current date with java

Get Current Date and Time in Java

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 and LocalTime classes.
  • For JDK 7 or earlier, we can use of Date and Calendar classes only.

1. Get Current Date and Time (Java 8 or Later)

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.

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

Читайте также:  Are sets immutable in python

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)

Till version 7 or earlier, Java didn’t have separate classes for the date and time parts. The main classes were :

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

Источник

How to get current date and time in Java

In this article, we will look at different ways to get the date and time in Java using both legacy classes ( Date & Calendar ) as well as Java 8 new date and time API.

Java 8 introduced a completely new date and time API (classes in the java.time.* package) to address the shortcomings of the existing API ( java.util.Date & java.util.Calendar ). The new API is not only thread-safe but also much more user-friendly, with tons of utility methods for performing different date and time tasks.

As the name suggests, the LocalDate class stores the date in the ISO-8601 format (yyyy-MM-dd) without any time or timezone information. This means that you can only get the current date in the system’s default timezone without time. Here is an example that shows how you can use LocalDate to get the current date:

// get current date LocalDate now = LocalDate.now(); // print date System.out.println("Current Date: " + now); // print date in a different format DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy"); System.out.println("Current Formatted Date: " + now.format(formatter)); 
Current Date: 2019-12-21 Current Formatted Date: Saturday, December 21, 2019 

The LocalTime class does the opposite of LocalDate . It stores the local time in ISO 8601 format without date or timezone information. This means that you can get the current time of the day without the actual date, as shown below:

// get current time LocalTime now = LocalTime.now(); // print time System.out.println("Current Time: " + now); // print time in a different format DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a"); System.out.println("Current Formatted Time: " + now.format(formatter)); 
Current Time: 03:30:08.116 Current Formatted Time: 03:30 AM 

The LocalDateTime class, the most popular date and time class in Java, holds both local date and time without any timezone information. Here is an example that demonstrates how you can use LocalDateTime to get the current date and time in Java 8 and higher:

// get the current date and time LocalDateTime now = LocalDateTime.now(); // print date and time System.out.println("Current Date & Time: " + now); // print the date and time in a different format DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy HH:mm:ss"); System.out.println("Current Formatted Date & Time: " + now.format(formatter)); 
Current Date & Time: 2019-12-21T03:36:47.324 Current Formatted Date & Time: Saturday, December 21, 2019 03:36:47 

Finally, ZonedDateTime is used to store both date and time along with the timezone information. Here is an example that shows how you can get the current zoned date and time using the system’s default timezone:

// get the current zoned date and time ZonedDateTime now = ZonedDateTime.now(); // print zoned date and time System.out.println("Current Zoned Date & Time: " + now); // print zoned date and time in a different format DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy HH:mm:ss Z"); System.out.println("Current Formatted Date & Time: " + now.format(formatter)); 
Current Zoned Date & Time: 2019-12-21T03:42:25.688+05:00[Asia/Karachi] Current Formatted Date & Time: Saturday, December 21, 2019 03:42:25 +0500 

To get the current date and time for a different timezone, you can use the ZoneId identifier as shown below:

// get the current Paris date and time ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Paris")); // print zoned date and time System.out.println("Current Paris Date & Time: " + now); 
Current Paris Date & Time: 2019-12-20T23:46:26.220+01:00[Europe/Paris] 

The Instant class represents a specific moment on the timeline. You can use this class to get the current UTC date and time as EPOCH seconds or milliseconds, as shown below:

// get current instance Instant now = Instant.now(); // print current instant System.out.println("Current Instant: " + now); // epoch seconds/millis System.out.println("EPOCH Seconds: " + now.getEpochSecond()); System.out.println("EPOCH Milliseconds: " + now.toEpochMilli()); 
Current Instant: 2019-12-20T22:52:00.870Z EPOCH Seconds: 1576882320 EPOCH Milliseconds: 1576882320870 

Another way of getting the current date and time in Java is using the legacy Date and Calendar classes. All you need to do is create an instance of Date , use SimpleDateFormat to create the desired format, and then pass the date object to the SimpleDateFormat.format() method to get the current date and time as a string.

To get the current date and time, you only need to instantiate the java.util.Date object. Optionally, if you want to display the current date and time in a different format, you can use the SimpleDateFormat class to format the Date object as shown below:

// get the current date and time Date now = new Date(); // print date object System.out.println("Current Date & Time: " + now); // print date object in a specific format SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy HH:mm"); System.out.println("Current Formatted Date & Time: " + format.format(now)); 
Current Date & Time: Sat Dec 21 11:33:44 PKT 2019 Current Formatted Date & Time: Saturday, December 21, 2019 11:33 
  • Create an instance of Calendar by calling the getInstance() static method.
  • Use Calendar.getTime() method to get the current date and time as a Date object.
  • Optionally, format the date using SimpleDateFormat to display it in a different format.
// create a calendar instance Calendar calendar = Calendar.getInstance(); // get the current date and time Date now = calendar.getTime(); // print date object System.out.println("Current Date & Time: " + now); // print date object in a specific format SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy HH:mm"); System.out.println("Current Formatted Date & Time: " + format.format(now)); 

As you can see above, using the Calendar class is as simple as using Date . You create a Calendar instance and then get the current date and time as a Date object. The rest of the code is similar to what we did in the previous example.

By default, the Date and Calendar classes return the current date and time in the default system timezone. To get the current date and time in a different timezone, you need to explicitly set the desired timezone. Here is an example that shows how you can set the timezone while formatting the Date object using SimpleDateFormat :

// get the current date and time Date now = new Date(); // format date and time in Europe/Paris timezone SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy HH:mm z"); format.setTimeZone(TimeZone.getTimeZone("Europe/Paris")); System.out.println("Current Paris Date & Time: " + format.format(now)); 
Current Paris Date & Time: Saturday, December 21, 2019 07:57 CET 

If you are using Calendar , you can use the Calendar.setTimeZone() method to change the default timezone to the one you want to, as shown below:

// create calendar instance Calendar c = Calendar.getInstance(); // set Europe/Paris timezone c.setTimeZone(TimeZone.getTimeZone("Europe/Paris")); // format date and time in a specific timezone SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy hh:mm a z"); format.setTimeZone(c.getTimeZone()); System.out.println("Current Paris Date & Time: " + format.format(c.getTime())); 
Current Paris Date & Time: Saturday, December 21, 2019 08:04 AM CET 

If you only want to get the current date and time as the number of milliseconds passed since the Unix Epoch, use System.currentTimeMillis() . This method returns the current time in milliseconds:

// get EPOCH milliseconds long millis = System.currentTimeMillis(); // print milliseconds System.out.println("EPOCH milliseconds: " + millis); 
EPOCH milliseconds: 1576915724838 

To convert the above milliseconds into human-readable format, you can do the following:

// convert EPOCH milliseconds to date Date date = new Date(System.currentTimeMillis()); // print date in human-readable format SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' HH:mm:ss z"); System.out.println("Current Date & Time: " + format.format(date)); 
Current Date & Time: Saturday, December 21, 2019 at 13:15:23 PKT 

There are many scenarios where you need the current date and time in Java. In this article, we have discussed almost all possible ways to get the current date and time in Java, including Java 8 new date and time API, legacy Date and Calendar classes, and more. The new date and time API provides an extensive set of classes that has simplified working with date and time in Java 8 and higher. These classes are thread-safe, easier to understand, and backward-compatible. If you are working on a legacy application that uses the old Date and Calendar API, you can easily convert the legacy code to the new date and time API. ✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like.

Источник

Оцените статью