java localdatetime format code example

Example 1: format localdate java

LocalDate today = LocalDate.now();
 
String formattedDate = today.format(DateTimeFormatter.ofPattern("dd-MMM-yy"));
 
System.out.println(formattedDate);

Example 2: format localdatetime

String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

Example 3: java localtime format

LocalTime time = LocalTime.parse("23:59:59"); 
  
 DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME; 

 String value = time.format(formatter);

Tags:

Java Example