Java Date Time - Java Local Date Time








LocalDate

LocalDate class represents a date without a time or time zone.

LocalDate is used when the time and time zone are related.

LocalDate class contains two constants, MAX and MIN.

MAX and MIN are the maximum and minimum supported LocalDate respectively.

LocalDate.MAX is +999999999-12-31 and LocalDate.MIN is -999999999-01-01.

The following code shows how to create LocalDate objects:

import java.time.LocalDate;
import java.time.Month;
//from   ww w  .j  av a2s  .  c om
public class Main {
  public static void main(String[] args) {
    // Get the current local date
    LocalDate localDate1  = LocalDate.now();
    System.out.println(localDate1);
    // Create a  local date
    LocalDate localDate2  = LocalDate.of(2014, Month.JUNE, 21);
    System.out.println(localDate2);
    // 10000  days after the epoch date 1970-01-01
    LocalDate localDate3  = LocalDate.ofEpochDay(10000);
    System.out.println(localDate3);
  }
}

The code above generates the following result.





Example

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

It creates Christmas days in next five years.

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. ja v a2 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.





LocalTime

LocalTime class represents a time without a date or time zone.

Time is represented to a nanosecond precision.

LocalTime class contains MIN, MAX, MIDNIGHT, and NOON constants

MIN is 00:00. MAX is 23:59:59.999999999. MIDNIGHT is 00:00. NOON is 12:00.

The following snippet of code creates LocalTime objects:

import java.time.LocalTime;
// w  w  w .  ja  v  a  2 s.  co m
public class Main {
  public static void main(String[] args) {
    // current
    LocalTime  localTime1 = LocalTime.now();
    System.out.println(localTime1);
    // 09:30
    LocalTime  localTime2 = LocalTime.of(9, 30);
    System.out.println(localTime2);
    // 09:30:50
    LocalTime  localTime3 = LocalTime.of(9, 30, 50);
    System.out.println(localTime3);
    // 09:30:50.000005678
    LocalTime  localTime4 = LocalTime.of(9, 30, 50, 5678);
    System.out.println(localTime4);
  }
}

The code above generates the following result.

LocalDateTime

LocalDateTime class represents a date and a time without a time zone.

LocalDateTime is a combination of LocalDate and LocalTime.

The following code shows how to create LocalDateTime objects:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
// w ww  .  ja  v  a  2s.c  o m
public class Main {
  public static void main(String[] args) {
    // current 
    LocalDateTime  localDateTime1 = LocalDateTime.now();
    System.out.println(localDateTime1);
    
    // 2014-06-21T16:12:34
    LocalDateTime  localDateTime2 = LocalDateTime.of(2014, Month.JUNE, 21, 16, 12, 34);
    System.out.println(localDateTime2);
    // from  a  local date and  a  local  time
    LocalDate localDate1  = LocalDate.of(2014, 5, 10); 
    LocalTime  localTime= LocalTime.of(16, 18,   41);
    LocalDateTime  localDateTime3 = LocalDateTime.of(localDate1, localTime);
    System.out.println(localDateTime3);
  }
}

The code above generates the following result.