
It represents date and time without timezone in ISO-8601 format. The LocalDateTime class is a popular choice for storing dates and times together in Java 8 and beyond. New Time Format: 23 Hours, 15 Minutes, 45 Seconds
#JAVA DATE FORMATTER CODE#
The above code will print the following on the console: Old Time Format: 23:15:45.456 println ( "New Time Format: " + newStr ) println ( "Old Time Format: " + oldStr ) System. ofPattern ( "HH 'Hours', mm 'Minutes', ss 'Seconds'" ) ) // print both strings System.

ofPattern ( "HH:mm:ss.SSS" ) ) // format time to string String newStr = time. Just like LocalDate, you can also use LocalTime and DateTimeFormatter to change the format of an existing time string as shown below: // old-time format String oldStr = "23:15:45.456" // parse old string to time LocalTime time = LocalTime. The output should look something like the below: 03:47:23 PM ofPattern ( "hh:mm:ss a" ) ) // print time string System. now ( ) // format time to string String timeStr = now. Here is an example that shows how you can format an instance of LocalTime: // current time LocalTime now = LocalTime. Here is the output of the above code snippet: Old Date Format: Ī LocalTime represents the time without date and timezone information. println ( "New Date Format: " + newStr ) println ( "Old Date Format: " + oldStr ) System. ofPattern ( "dd-MMM-yyyy" ) ) // print both strings System. ofPattern ( "MM/dd/yyyy" ) ) // format date to string String newStr = date. The following code snippet shows how you can parse a string in MM/dd/yyyy format to dd-MMM-yyyy format using both LocalDate and DateTimeFormatter: // old string format String oldStr = "" // parse old string to date LocalDate date = LocalDate. You can also use DateTimeFormatter to change the format of a date string. The above code will output the following: Sunday, December 29, 2019 ofPattern ( "EEEE, MMMM dd, yyyy" ) ) // print date string System. now ( ) // format date to string String dateStr = now. The following example demonstrates how you can format the current LocalDate into a date string: // current date LocalDate now = LocalDate. The LocalDate class represents the date without time in ISO-8601 format (yyyy-MM-dd). Let us look at the LocalDate and LocalDateTime examples below to see how it works.

DateTimeFormatter is used to specify a pattern for formatting and parsing date-time objects in Java 8 new date and time API. The format() method returns the formatted date string. Java 8 new date and time API provides a format() method that accepts an instance of DateTimeFormatter to format this date using the specified format: format ( DateTimeFormatter formatter ) All classes in the new date and time API use ISO-8601 format to represent dates and times. Java 8 new API is thread-safe, easier to use, and provides utility methods for date and time operations. Java 8 Date & Time APIĪ new date and time API was introduced in Java 8 (classes in the java.time.* package) to fix the flaws of the old and APIs. Likewise, you may want to convert a date from MM/dd/yyyy to dd-MMM-yyyy or any other format of your choice. There are many scenarios where you want to convert an instance of date to a more human-readable format.įor instance, you may have a date like T15:23, and you want to change its format to Decem03:23 PM. In this article, you'll learn to change the format of a given date or a string to a new string format using both Java 8 new date and time API and legacy API. In my previous article, we looked at different ways to convert a string to a date using Java 8 new date and time API as well as legacy Date and Calendar API.
