📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will see the following LocalDateTime format examples:
- Java format LocalDateTime to yyyy-MM-dd HH:mm:ss
- Java format LocalDateTime to yyyy/MM/dd HH:mm:ss
- Java format LocalDateTime to dd/MM/yyyy HH:mm:ss
- Java format LocalDateTime to dd-MM-yyyy HH:mm:ss
Java format LocalDateTime to yyyy-MM-dd 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("yyyy-MM-dd HH:mm:ss");
String ldt1 = formatterLocalDateTime.format(localDateTime);
// or shortly
String ldt2 = LocalDateTime.now()
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(ldt1);
System.out.println(ldt2);
}
}
Output:
2021-11-10 17:23:16
2021-11-10 17:23:16
Java format LocalDateTime to yyyy/MM/dd 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("yyyy/MM/dd HH:mm:ss");
String ldt1 = formatterLocalDateTime.format(localDateTime);
// or shortly
String ldt2 = LocalDateTime.now()
.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
System.out.println(ldt1);
System.out.println(ldt2);
}
}
Output:
2021/11/10 17:24:16 2021/11/10 17:24:16
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
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
Comments
Post a Comment
Leave Comment