Create Christmas days in next five years

Description

The following code shows how to combine a Year and MonthDay to get a LocalDate.

It creates Christmas days in next five years.

Example

The following code creates a MonthDay for December 25 and keeps combining a year to it to get a LocalDate.


import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;
import java.time.Year;
import java.time.format.TextStyle;
import java.util.Locale;
//from  w ww. j av  a 2  s  . c o  m
public class Main {

  public static void main(String[] args) {
    MonthDay dec25 = MonthDay.of(Month.DECEMBER, 25);
    Year year = Year.now();

    for (int i = 1; i <= 5; i++) {
      LocalDate ld = year.plusYears(i).atMonthDay(dec25);
      int yr = ld.getYear();
      String weekDay = ld.getDayOfWeek().getDisplayName(TextStyle.FULL,
          Locale.getDefault());
      System.out.format("Christmas in  %d  is on  %s.%n", yr, weekDay);
    }
  }
}

The code above generates the following result.





















Home »
  Java Date Time »
    Example »




Convert
Date
Timezone