Java Problems with Solutions
Find Duration in Hours and Minutes Between Two Dates in Java
Sort a Java List Alphabetically First and then by Numbers
Encryption and Decryption of Strings in Java
Generate a Random String of Specific Length in Java
Split a String by Comma and Space in Java
Remove Duplicates from a Java List
Converting HashSet to List in Java
Calculate Days Between Two Dates in Java
Round a Number to N decimal Places in Java
Java Run Multiple Tasks Simultaneously
Filling Values in Editable PDF Forms in Java
Create ICS Calendar File in Java/Spring Boot
Convert LocalDateTime to Milliseconds in Java
Convert String DateTime To LocalDateTime in Java
Convert LocalDateTime to String in Java
Convert JSON Array to List of Long Type in Java
Convert a List to an Array in Java
Convert an Array to a List in Java
Convert POJO Class Object to JSON String in Java
Convert JSON String to POJO Class Object in Java
How to Add Data to a List in Java
How to avoid Deadlock in Java with Example
Counting Unique Numbers in a List of Integers
Extract Created and Modified Datetime of Files in Java
Remove Matching Items from List Ignoring Case
Java Solved Programs and Problems with Solutions
Java LocalDateTime Formatting
Merge Sets and Convert to List in Java
Working with CSV files in Java: Read and Write Data with Ease
Resolving Java Type Mismatch Error: Cannot Convert from List<int[]> to List<Integer>
Validating Email with Regex in Java
Java LocalDateTime Formatting
- Last updated Apr 25, 2024
In this tutorial, you will learn how to format LocalDateTime objects in Java.
Here's an example that formats current datetime into a specific pattern using DateTimeFormatter:
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeFormattingExample { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println("Before formatting : " + now); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a"); String formattedDateTime = now.format(formatter); System.out.println("After formatting : " + formattedDateTime); } }
The output of the above code is as follows:
Before formatting : 2022-01-08T12:21:16.450152400 After formatting : 2022-01-08 12:21:16 PM
Here's an additional example that demonstrates how to format a String representing a datetime into a LocalDateTime object:
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeFormattingExample { public static void main(String[] args) { String stringDateTime = "16-01-2022 10:34:45 PM"; System.out.println("Before formatting : " + stringDateTime); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm:ss a"); LocalDateTime formattedLocalDateTime = LocalDateTime.parse(stringDateTime, formatter); System.out.println("After formatting : " + formattedLocalDateTime); } }
The output of the above code is as follows:
Before formatting : 16-01-2022 10:34:45 PM After formatting : 2022-01-16T22:34:45