🎓 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 (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
yyyy-MM-dd format is widely used and can be achieved using various classes from the java.time package, which was introduced in Java 8. This guide will cover different ways to get the current date in yyyy-MM-dd format, including using LocalDate and DateTimeFormatter.Table of Contents
- Introduction
- Using
LocalDateandDateTimeFormatter - Using
SimpleDateFormat(Java 7 and earlier) - Conclusion
Introduction
Java provides several classes to work with dates and times. Since Java 8, the java.time package provides a modern API for date and time handling. For applications that need to be compatible with earlier versions of Java, the SimpleDateFormat class can be used.
Using LocalDate and DateTimeFormatter
The LocalDate class represents a date without a time-zone in the ISO-8601 calendar system and the DateTimeFormatter class is used for formatting dates.
Example
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateFormatExample {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = currentDate.format(formatter);
System.out.println("Current date in yyyy-MM-dd format: " + formattedDate);
}
}
Explanation
LocalDate.now(): Obtains the current date from the system clock in the default time-zone.DateTimeFormatter.ofPattern("yyyy-MM-dd"): Creates a formatter using the specified pattern.currentDate.format(formatter): Formats the current date using the specified formatter.
Output:
Current date in yyyy-MM-dd format: 2023-08-25
Using SimpleDateFormat (Java 7 and Earlier)
For applications that need to be compatible with Java 7 or earlier, the SimpleDateFormat class can be used to format dates.
Example
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Date currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(currentDate);
System.out.println("Current date in yyyy-MM-dd format: " + formattedDate);
}
}
Explanation
new Date(): Creates aDateobject representing the current date and time.new SimpleDateFormat("yyyy-MM-dd"): Creates a formatter using the specified pattern.formatter.format(currentDate): Formats the current date using the specified formatter.
Output:
Current date in yyyy-MM-dd format: 2023-08-25
Conclusion
Formatting dates in Java to the yyyy-MM-dd format can be accomplished using various methods:
- Using
LocalDateandDateTimeFormatterprovides a modern and efficient way to handle dates and formatting in Java 8 and later. - Using
SimpleDateFormatis suitable for applications that need to be compatible with Java 7 or earlier.
By understanding these methods, you can choose the most appropriate one for your specific use case when working with date formatting in Java.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment