Java: Get Date in yyyy-MM-dd Format

In Java, formatting dates to a specific pattern is a common requirement. The 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

  1. Introduction
  2. Using LocalDate and DateTimeFormatter
  3. Using SimpleDateFormat (Java 7 and earlier)
  4. 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 a Date object 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 LocalDate and DateTimeFormatter provides a modern and efficient way to handle dates and formatting in Java 8 and later.
  • Using SimpleDateFormat is 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.

Comments