Java format() method is used to format date-time in a specific format. It formats the date-time into the specified format and returns a date-time string. For example, if we want to format a date in 'dd/mm/yyyy' format then this method is suitable.
It takes a DateTimeFormatter object to specify the date-time format and returns a string.
This format() method is a LocalDateTime class method that is stored in java.time package. The syntax of the format() method is the following:
public String format(DateTimeFormatter formatter)
It takes DateTimeFormatter to format the date-time in a specific format.
It returns a date-time string.
Here, we are formatting localdatetime using the format() method. Since, format() method takes DateTimeFormatter, we are passing a new format pattern by using ofPattern() method of DateTimeFormatter class. This example prints a new date-time format.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/*
* Example to format LocalDateTime
*/
public class JExercise {
public static void main(String[] args) {
// Create a LocalDateTime date
LocalDateTime datetime = LocalDateTime.parse("2012-05-02T02:30:10");
// Print Date and time
System.out.println("Date and Time : "+datetime);
// Formatting date-time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// Calling format() method
String newformat = datetime.format(formatter); // Returns a string
System.out.println(newformat);
}
}
Output:
Date and Time : 2012-05-02T02:30:10
2012-05-02 02:30:10
The format() method allows us to format the date-time in any standard date format. Here, we are formatting the localdatetime into dd/mm/yyyy format.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/*
* Example to format LocalDateTime in dd/mm/yyyy format
*/
public class JExercise {
public static void main(String[] args) {
// Create a LocalDateTime date
LocalDateTime datetime = LocalDateTime.parse("2012-05-02T02:30:10");
// Print Date and time
System.out.println("Date and Time : "+datetime);
// Formatting date-time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
// Calling format() method
String newformat = datetime.format(formatter); // Returns a string
System.out.println(newformat);
}
}
Output:
Date and Time : 2012-05-02T02:30:10
02-05-2012 02:30:10
Since the format() method returns a date-time of string type. So we need to convert it back to LocalDateTime for further operations. Here, we are converting a string to LocalDateTime by using the parse() method of LocalDateTime class. The parse() method returns a LocalDateTime object after converting string date-time.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/*
* Example to format LocalDateTime in dd/mm/yyyy format
*/
public class JExercise {
public static void main(String[] args) {
// Create a LocalDateTime date
LocalDateTime datetime = LocalDateTime.parse("2012-05-02T02:30:10");
// Print Date and time
System.out.println("Date and Time : "+datetime);
// Formatting date-time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
// Calling format() method
String newformat = datetime.format(formatter); // Returns a string
System.out.println(newformat);
// Converting String to LocalDatetime back
datetime = LocalDateTime.parse(newformat,formatter);
System.out.println(datetime);
}
}
Output:
Date and Time : 2012-05-02T02:30:10
02-05-2012 02:30:10
2012-05-02T02:30:10
Well, in this topic, we learnt To format a LocalDateTime into a specified format by using the format() method. We also learnt to convert string date-time to LocalDateTime. We included several methods to understand the format() method better.
If we missed something, you can suggest us at - info.javaexercise@gmail.com