In this source code example, we will you how to format a LocalDateTime into dd-MM-yyyy HH:mm:ss format in Java.
Java format LocalDateTime to dd-MM-yyyy HH:mm:ss
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// yyyy-MM-dd HH:mm:ss
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatterLocalDateTime =
DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String ldt1 = formatterLocalDateTime.format(localDateTime);
// or shortly
String ldt2 = LocalDateTime.now()
.format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"));
System.out.println(ldt1);
System.out.println(ldt2);
}
}
Output:
10-11-2021 17:28:37 10-11-2021 17:28:37
Free Spring Boot Tutorial - 5 Hours Full Course
Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course