Java Date Time - Java Year Month Day








Year

A Year represents a year, for example, 2012, 2013, etc.

The following code shows how to create a Year object and perform basic operations on them.

import java.time.Year;
/*w w w  .ja va 2  s  .c o  m*/
public class Main {

  public static void main(String[] args) {
    Year y1 = Year.of(2014);
    System.out.println(y1);
    Year y2 = y1.minusYears(1);
    System.out.println(y2);
    Year y3 = y1.plusYears(1);
    System.out.println(y3);
    Year y4 = Year.now();
    System.out.println(y4);
    if (y1.isLeap()) {
      System.out.println(y1 + "  is a  leap year.");
    } else {
      System.out.println(y1 + "  is not  a  leap year.");
    }

  }
}

The code above generates the following result.





YearMonth

A YearMonth represents a valid combination of a year and a month, for example, 2012-05, 2013-09, etc.

The following code shows how to create YearMonth objects and perform some basic operatioins on them.

import java.time.Month;
import java.time.YearMonth;
//from w  w w  .  j a  v a2 s.co  m
public class Main {

  public static void main(String[] args) {
    YearMonth ym1 = YearMonth.of(2014, Month.JUNE);  

    int monthLen = ym1.lengthOfMonth(); 
    System.out.println(monthLen);

    int yearLen  = ym1.lengthOfYear();
    System.out.println(yearLen);
  }
}

The code above generates the following result.





Month

Month enum has 12 constants to represents the 12 months.

The constant names are JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, and DECEMBER.

Month enum are numbered sequentially from 1 to 12, where January is 1 and December is 12.

Month enum of() creates an instance of Month from an int value.

We can use from() to create the Month from date object.

To get the int value of the Month use Month enum getValue() method.

import java.time.LocalDate;
import java.time.Month;
/*  w ww .ja v a2s . c  o m*/
public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = LocalDate.of(2014, Month.AUGUST, 3);
    System.out.println(localDate);
    Month month1  = Month.from(localDate);
    System.out.println(month1);
    
    Month month2  = Month.of(2);
    System.out.println(month2);
    
    Month month3  = month2.plus(2);
    System.out.println(month3);
    
    Month month4  = localDate.getMonth();
    System.out.println(month4);
    
    int monthIntValue  = month2.getValue();
    System.out.println(monthIntValue);
  }
}

The code above generates the following result.

MonthDay

A MonthDay represents a valid combination of a month and a day of month, for example, 12-15.

The following code shows how to create MonthDay objects and perform basic operations on them.

import java.time.Month;
import java.time.MonthDay;
//from   www  .j a v  a2 s. c o m
public class Main {

  public static void main(String[] args) {
    MonthDay md1 = MonthDay.of(Month.DECEMBER, 25);
    MonthDay md2 = MonthDay.of(Month.FEBRUARY, 29);

    if (md2.isValidYear(2014)) {
      System.out.println(md2);
    }
    System.out.println(md1.getDayOfMonth());

  }
}

The code above generates the following result.

DayOfWeek

DayOfWeek enum defines seven constants to represent seven days in a week.

The constants from DayOfWeek enum are MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY.

The backend an int value is from 1 to 7. 1 for Monday, 2 for Tuesday...

The following code shows how to use the DayOfWeek enum.

import java.time.DayOfWeek;
import java.time.LocalDate;
//from  w  w w .ja va 2  s.  c o  m
public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = LocalDate.of(2014, 6, 21);
    System.out.println(localDate);
    DayOfWeek  dayOfWeek1 = DayOfWeek.from(localDate);
    System.out.println(dayOfWeek1);
    int intValue = dayOfWeek1.getValue();
    System.out.println(intValue);
    DayOfWeek  dayOfWeek2 = localDate.getDayOfWeek();
    System.out.println(dayOfWeek2);
    DayOfWeek  dayOfWeekFromInteger = DayOfWeek.of(7);
    System.out.println(dayOfWeekFromInteger);
    DayOfWeek  dayOfWeekAdded = dayOfWeekFromInteger.plus(1);
    System.out.println(dayOfWeekAdded);
    DayOfWeek  dayOfWeekSubtracted = dayOfWeekFromInteger.minus(2); 
    System.out.println(dayOfWeekSubtracted);
  }
}

The code above generates the following result.