Calendar date to yyyy-MM-dd format in java
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Java provides robust support for date and time manipulation primarily through the java.time package introduced in Java 8, known as the Java Date and Time API. Before Java 8, handling date and time was cumbersome due to various issues in the old java.util.Date and java.util.Calendar classes. The new API addresses these concerns and offers a cleaner and more comprehensive way to handle date and time. This article details how to convert a calendar date to the yyyy-MM-dd format in Java using the modern API.
Introduction to the java.time Package
Java's java.time package is centered around several core classes:
LocalDate: Represents a date without time-zone.LocalTime: Represents a time without a time-zone.LocalDateTime: Represents both date and time, without a time-zone.ZonedDateTime: Represents a date and time with a time-zone.
For the purpose of formatting dates to yyyy-MM-dd, we primarily work with the LocalDate and the DateTimeFormatter classes. LocalDate captures only dates without associated time-of-day, making it ideal for formatting in a date-specific manner.
Example: Formatting a Date to yyyy-MM-dd
Let's illustrate the conversion of a calendar date to the yyyy-MM-dd format with a practical example using LocalDate.
Explanation
- Obtaining the Current Date: We use
LocalDate.now()to get the current date. This method does not account for time-zone which makes it suitable when time isn't a factor in operations. - Defining the Formatter:
DateTimeFormatter.ofPattern("yyyy-MM-dd")creates a formatter that specifies the needed output format. Note thatyyyyrepresents the year,MMthe month, andddthe day. - Formatting the Date: The
format()method converts theLocalDateinto aStringfollowing the defined pattern.
Alternative Sources
If you are dealing with a java.util.Date instance, you can convert it to LocalDate as shown below:
Conversion Table
| Step | Description |
| LocalDate Initialization | Use LocalDate.now() for the current date calculation. |
| Format Pattern Definition | Define using DateTimeFormatter.ofPattern("yyyy-MM-dd"). |
| Formatting Method | Use .format(DateTimeFormatter) to convert LocalDate to a formatted string. |
| Date Conversion | Convert java.util.Date to LocalDate using toInstant(), atZone(),
and toLocalDate(). |
Additional Considerations
- Time Zones: If you need to consider time-zones,
ZonedDateTimeis recommended. - Locale-Specific Formats: Date formatting can be locale-specific. The
withLocale()method onDateTimeFormatterallows specifying a locale.
- Thread Safety:
DateTimeFormatteris immutable and thread-safe. This enables it to be safely used in a multi-threaded environment without external synchronization.
Using the java.time package drastically simplifies date-time operations in Java, paving the way for clearer and more maintainable code. Always prefer the java.time classes over legacy date-time classes for any new development.

