Google News
logo
Java Program to Display Dates of a Calendar Year in Different Formats
In the following example of Java program that displays dates of a calendar year in different formats using the java.time package :
Program :
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class CalendarDates {
    public static void main(String[] args) {
        // Create a LocalDate object for the first day of the year
        LocalDate date = LocalDate.ofYearDay(2020, 1);
        
        // Loop through all the days of the year
        while (date.getYear() == 2020) {
            // Format the date in different formats
            String formatted1 = date.format(DateTimeFormatter.ISO_LOCAL_DATE);
            String formatted2 = date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
            String formatted3 = date.format(DateTimeFormatter.ofPattern("EEE, MMM d, yyyy"));
            
            // Print the formatted dates
            System.out.println(formatted1 + " | " + formatted2 + " | " + formatted3);
            
            // Move to the next day
            date = date.plusDays(1);
        }
    }
}
Output :
2020-01-01 | 01-01-2020 | Wed, Jan 1, 2020
2020-01-02 | 02-01-2020 | Thu, Jan 2, 2020
2020-01-03 | 03-01-2020 | Fri, Jan 3, 2020
2020-01-04 | 04-01-2020 | Sat, Jan 4, 2020
2020-01-05 | 05-01-2020 | Sun, Jan 5, 2020
2020-01-06 | 06-01-2020 | Mon, Jan 6, 2020
2020-01-07 | 07-01-2020 | Tue, Jan 7, 2020
2020-01-08 | 08-01-2020 | Wed, Jan 8, 2020
2020-01-09 | 09-01-2020 | Thu, Jan 9, 2020
2020-01-10 | 10-01-2020 | Fri, Jan 10, 2020
2020-01-11 | 11-01-2020 | Sat, Jan 11, 2020
2020-01-12 | 12-01-2020 | Sun, Jan 12, 2020
2020-01-13 | 13-01-2020 | Mon, Jan 13, 2020
2020-01-14 | 14-01-2020 | Tue, Jan 14, 2020
2020-01-15 | 15-01-2020 | Wed, Jan 15, 2020
2020-01-16 | 16-01-2020 | Thu, Jan 16, 2020
2020-01-17 | 17-01-2020 | Fri, Jan 17, 2020
2020-01-18 | 18-01-2020 | Sat, Jan 18, 2020
2020-01-19 | 19-01-2020 | Sun, Jan 19, 2020
2020-01-20 | 20-01-2020 | Mon, Jan 20, 2020
2020-01-21 | 21-01-2020 | Tue, Jan 21, 2020
2020-01-22 | 22-01-2020 | Wed, Jan 22, 2020
2020-01-23 | 23-01-2020 | Thu, Jan 23, 2020
2020-01-24 | 24-01-2020 | Fri, Jan 24, 2020
2020-01-25 | 25-01-2020 | Sat, Jan 25, 2020
2020-01-26 | 26-01-2020 | Sun, Jan 26, 2020
2020-01-27 | 27-01-2020 | Mon, Jan 27, 2020
2020-01-28 | 28-01-2020 | Tue, Jan 28, 2020
2020-01-29 | 29-01-2020 | Wed, Jan 29, 2020
2020-01-30 | 30-01-2020 | Thu, Jan 30, 2020
2020-01-31 | 31-01-2020 | Fri, Jan 31, 2020
2020-02-01 | 01-02-2020 | Sat, Feb 1, 2020
2020-02-02 | 02-02-2020 | Sun, Feb 2, 2020
2020-02-03 | 03-02-2020 | Mon, Feb 3, 2020
2020-02-04 | 04-02-2020 | Tue, Feb 4, 2020
2020-02-05 | 05-02-2020 | Wed, Feb 5, 2020
2020-02-06 | 06-02-2020 | Thu, Feb 6, 2020
2020-02-07 | 07-02-2020 | Fri, Feb 7, 2020
2020-02-08 | 08-02-2020 | Sat, Feb 8, 2020
2020-02-09 | 09-02-2020 | Sun, Feb 9, 2020
2020-02-10 | 10-02-2020 | Mon, Feb 10, 2020
2020-02-11 | 11-02-2020 | Tue, Feb 11, 2020
2020-02-12 | 12-02-2020 | Wed, Feb 12, 2020
2020-02-13 | 13-02-2020 | Thu, Feb 13, 2020
2020-02-14 | 14-02-2020 | Fri, Feb 14, 2020
2020-02-15 | 15-02-2020 | Sat, Feb 15, 2020
2020-02-16 | 16-02-2020 | Sun, Feb 16, 2020
2020-02-17 | 17-02-2020 | Mon, Feb 17, 2020
2020-02-18 | 18-02-2020 | Tue, Feb 18, 2020
2020-02-19 | 19-02-2020 | Wed, Feb 19, 2020
2020-02-20 | 20-02-2020 | Thu, Feb 20, 2020
2020-02-21 | 21-02-2020 | Fri, Feb 21, 2020
2020-02-22 | 22-02-2020 | Sat, Feb 22, 2020
2020-02-23 | 23-02-2020 | Sun, Feb 23, 2020
2020-02-24 | 24-02-2020 | Mon, Feb 24, 2020
2020-02-25 | 25-02-2020 | Tue, Feb 25, 2020
2020-02-26 | 26-02-2020 | Wed, Feb 26, 2020
2020-02-27 | 27-02-2020 | Thu, Feb 27, 2020
2020-02-28 | 28-02-2020 | Fri, Feb 28, 2020
2020-02-29 | 29-02-2020 | Sat, Feb 29, 2020
2020-03-01 | 01-03-2020 | Sun, Mar 1, 2020
2020-03-02 | 02-03-2020 | Mon, Mar 2, 2020
2020-03-03 | 03-03-2020 | Tue, Mar 3, 2020
2020-03-04 | 04-03-2020 | Wed, Mar 4, 2020
2020-03-05 | 05-03-2020 | Thu, Mar 5, 2020
2020-03-06 | 06-03-2020 | Fri, Mar 6, 2020
2020-03-07 | 07-03-202
This program creates a LocalDate object for the first day of the year using the ofYearDay() method of the LocalDate class. It then loops through all the days of the year using a while loop, formatting each date in three different formats using the format() method of the DateTimeFormatter class with different formatter objects.

Finally, it prints the formatted dates to the console in a table-like format.