
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- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- How to reset AirPods or AirPods Pro using iPhone/iPad or iPod - iOS
- Installing Native Chrome Browser App on M1 Mac Device - Chrome
- How to update VIM version on a Mac - vi
- Change CKEditor Table Properties default width - CKEditor
- 12 August - International Youth Day celebrated worldwide - News
- Check macOS free disk space using Terminal command - MacOS
- pwd Command - Print Working Directory - Linux
- How to get list of all Java versions installed on macOS - Java