Java Date Time - Month Example








Example

The following code shows how to get all of the Mondays in the current year and the specified month.

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.time.temporal.TemporalAdjusters;
//  w  ww  .  ja  va  2  s.  c  om
public class Main {
    public static void main(String[] args) {
        Month month = Month.valueOf("March".toUpperCase());

        System.out.printf("For the month of %s:%n", month);
        LocalDate date = Year.now().atMonth(month).atDay(1).
              with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
        Month mi = date.getMonth();
        while (mi == month) {
            System.out.printf("%s%n", date);
            date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
            mi = date.getMonth();
        }
    }
}

The code above generates the following result.





Field

  1. Month JANUARY
  2. Month FEBRUARY
  3. Month MARCH
  4. Month APRIL
  5. Month MAY
  6. Month JUNE
  7. Month JULY
  8. Month AUGUST
  9. Month SEPTEMBER
  10. Month OCTOBER
  11. Month NOVEMBER
  12. Month DECEMBER




Method

  1. Month adjustInto(Temporal temporal)
  2. Month firstDayOfYear(boolean leapYear)
  3. Month firstMonthOfQuarter()
  4. Month from(TemporalAccessor temporal)
  5. Month getDisplayName(TextStyle style, Locale locale)
  6. Month getLong(TemporalField field)
  7. Month getValue()
  8. Month get(TemporalField field)
  9. Month isSupported(TemporalField field)
  10. Month length(boolean leapYear)
  11. Month maxLength()
  12. Month minLength()
  13. Month minus(long months)
  14. Month of(int month)
  15. Month plus(long months)
  16. Month query(TemporalQuery query)
  17. Month range(TemporalField field)
  18. Month valueOf(String name)
  19. Month values()