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- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- Tailwind CSS Hello World Example - CSS
- Dynamically Obtaining Browser Screen Width and Height with jQuery [Updated 2023] - jQuery
- Fix- Microsoft Word Pages Appear Black - Microsoft
- Bash For Loop Example - Bash
- Setting Java_Home Environment variable on Windows Operating System - Java
- How to Add Edit with Notepad++ Option to Windows 10 or 11 Right Click Menu Options - Windows-11
- This Toast was not created with Toast.makeText() : Android RuntimeException - Android
- sudo is not recognized as an internal or external command - Windows