
Java 8 brought in the much-awaited Date & Time API (JEP 150) with the new java.time package with a whole new set of Date and Time classes, with the new class, also came a new way of formatting Date and Time, let us look at some examples,
Example: Get the current local date and time - Java 8LocalDateTime localDateTime = LocalDateTime.now(); //ISO-8601: yyyy-MM-ddTHH:mm:ss.S
System.out.println(localDateTime);
2022-04-24T12:21:31.113921
As you can see when by default the date is printed in yyyy-MM-dd format and the time in HH:mm:ss.S format. You can make use of the DataTimeFormatter class from java.time.format package.
Example 1: Format Date in dd-MM-yyyy format - Java 8
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate = LocalDate.now();
System.out.println(dateTimeFormatter.format(localDate));
Output: 24-04-2022
Example 2: Format Date in MM-dd-yy format - Java 8
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yy");
LocalDate localDate = LocalDate.now();
System.out.println(dateTimeFormatter.format(localDate));
Output: 04-24-22
Example 3: Format Date in dd-MMM-yyyy format - Java 8
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
LocalDate localDate = LocalDate.now();
System.out.println(dateTimeFormatter.format(localDate));
Output: 24-Apr-2022
Example 4: Format Date in dd, MMMM yyyy format - Java 8
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd, MMMM yyyy");
LocalDate localDate = LocalDate.now();
System.out.println(dateTimeFormatter.format(localDate));
Output: 24, April 2022
Now let us see some examples of formatting LocalTime using DateTimeFormatter,
Example 1: Format Time in HH:mm format - Java 8
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime localTime = LocalTime.now();
System.out.println(dateTimeFormatter.format(localTime));
Output: 12:24
Example 2: Format Time in HH:mm:ss format - Java 8
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.now();
System.out.println(dateTimeFormatter.format(localTime));
Output: 12:25:24
Finally, a few examples of formatting LocalDateTime class using DateTimeFormatter,
Example 1: Format DateTime in dd-MM-yyyy HH:mm:ss format - Java 8
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(dateTimeFormatter.format(localDateTime));
Output: 24-04-2022 12:47:53
Example 2: Format DateTime in MM-dd-yyyy HH:mm format - Java 8
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm");
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(dateTimeFormatter.format(localDateTime));
Output: 04-24-2022 12:50
Read more: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
- Remove Trailing zeros BigDecimal Java - Java
- How to change background color in Notepad++ - NotepadPlusPlus
- How to Create Absolute References in Microsoft Excel for Mac - Microsoft
- 🎃 Trending, Popular Halloween hashtags for year 2020 🎃 [Facebook, Twitter, Instagram, Snapchat] - Hashtags
- How to refresh Safari on Mac (macOS) using keyboard shortcut - MacOS
- How to empty Trash in macOS? - MacOS
- Bash Command to Check Python Version - Bash
- Android SecurityException: Need BLUETOOTH ADMIN permissicacheNameAndAddresson: Neither user 10123 nor current process has android.permission.BLUETOOTH_ADMIN - Android