Java current date formatted

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.

Читайте также:  Css background size top

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.

Читайте также:  Модули python для работы с excel

You might also like.

Источник

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