Site Search:

Create and manipulate calendar data using classes from java.time.LocalDateTime, java.time.LocalDate, java.time.LocalTime


Back OCAJP



In this post, we study how to handle time with java.time.LocalDateTime, java.time.LocalDate and java.time.LocalTime. They are immutable, we use static methods of()  to create new objects. Let's study by examples.


OCAJP>cat test.java
import java.time.*; 
class test {
  public static void main(String... args) {
    /*2016-03-13
      23:15:27.195
      2016-03-13T23:15:27.195
    */
    System.out.println("now()");
    System.out.println(LocalDate.now());  //yyyy-MM-dd
    System.out.println(LocalTime.now());  //HH:mm:ss.sss
    System.out.println(LocalDateTime.now());  //yyyy-mm-ddTHH:mm:ss.sss
    System.out.println("of()");
    System.out.println(LocalDate.of(2016, 1, 1));  //year, month, day
    //System.out.println(LocalDate.of(2016, 1, 1000));  //compile, but throw runtime exception
    System.out.println(LocalTime.of(1,1,1));  //24 hour, minutes, seconds, no timezone
    System.out.println(LocalDateTime.of(2016,1,1,1,1,1));  //date + time, no timezone
    /*
    Month.JANUARY, Month.FEBERARY, Month.MARCH, Month.APRAIL, etc.
    Note Month.JANUARY is 1, while Calendar.JANUARY is 0.
    */
    System.out.println("Month enum");
    LocalDate d = LocalDate.of(2016, Month.JANUARY, 1);
    LocalTime t = LocalTime.of(23, 10, 10, 999);  //nanosecond
    //LocalDateTime dt = LocalDateTime(d,t); //not compile
    LocalDateTime dt = LocalDateTime.of(d,t);
    System.out.println(dt);
    System.out.println("plusxxx() minusxxx()");
    System.out.println(LocalDate.of(2016, 1, 1).plusYears(1).plusMonths(1).plusDays(1));
    System.out.println(LocalDate.of(2016, 1, 1).minusYears(1).minusMonths(1).minusDays(1).minusWeeks(1));
    System.out.println(LocalTime.of(1,1,1).plusHours(1).plusMinutes(1).plusSeconds(1));
    System.out.println(LocalTime.of(1,1,1).minusHours(1).minusMinutes(1).minusSeconds(1).plusNanos(1));
    System.out.println(LocalTime.of(1,1,1).minusHours(1).minusMinutes(1).minusSeconds(1).minusNanos(1));
    System.out.println(LocalDateTime.now().minusYears(3).plusWeeks(3).minusHours(2).plusNanos(100));
    //LocalDate.of(2010,1,1).plusHours(1);  //not compile
    //LocalTime.of(12,1,1).plusYears(1);  //not compile
  }
}
OCAJP>javac test.java 
OCAJP>java test
now()
2016-03-13
23:46:52.277
2016-03-13T23:46:52.277
of()
2016-01-01
01:01:01
2016-01-01T01:01:01
Month enum
2016-01-01T23:10:10.000000999
plusxxx() minusxxx()
2017-02-02
2014-11-23
02:02:02
00:00:00.000000001
23:59:59.999999999
2013-04-03T21:46:52.278000100

LocalDateTime's plus* and minus* methods are used to add a time period; alternatively, we can use plus(Period p) and minus(Period p) methods, where Period represents a time span.
Lets take a look at examples.


OCAJP>cat test.java 
import java.time.*; 
class test {
  public static void main(String... args) {
    Period p = Period.ofYears(3);  //static ofYear() is a static method
    //LocalDateTime dt = new LocalDateTime.now();  //not compile
    LocalDateTime dt = LocalDateTime.now();
    System.out.println(dt);
    System.out.println(dt.plus(p));  //plus returns a reference of LocalDateTime
    p = Period.ofDays(2); 
    System.out.println(dt.minus(p));
    System.out.println(dt);  //dt never changed value
    System.out.println(dt.plus(Period.ofYears(100).ofDays(1)));  //Period is static method, only the last call takes effect
    //Period.ofHours(10);  //not compile
    p = Period.of(1, 1, 1);
    System.out.println(dt.plus(p)); //Peroid is 1 year, 1 month, 1 day
  }
}
OCAJP>javac test.java
OCAJP>java test
2016-03-14T00:28:04.261
2019-03-14T00:28:04.261
2016-03-12T00:28:04.261
2016-03-14T00:28:04.261
2016-03-15T00:28:04.261
2017-04-15T00:28:04.261


Finally, let's study java.time.format.DateTimeFormater
In order to print out a LocalDate, LocalDateTime, LocalTime in a certain format, we have several ways of doing that.


OCAJP>cat test.java 
import java.time.*; 
import java.time.format.*;
class test {
  public static void main(String... args) {
    LocalDate date = LocalDate.of(2016, 1, 1);
    System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
    LocalTime time = LocalTime.of(10, 1, 1);
    //standard format
    System.out.println(time.format(DateTimeFormatter.ISO_LOCAL_TIME));
    LocalDateTime datetime = LocalDateTime.of(date,time);
    System.out.println(datetime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    
    //ofLocalized* format
    DateTimeFormatter formater = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
    System.out.println(datetime.format(formater));
    //System.out.println(date.format(DateTimeFormatter.ofLocalizedDate()));  //required: FormatStyle
    //System.out.println(date.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM)));  //java.time.temporal.UnsupportedTemporalTypeException
    System.out.println(datetime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM)));    
    System.out.println(time.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM)));
   
    //ofPattern free format 
    System.out.println(datetime.format(DateTimeFormatter.ofPattern("MMM dd yyyy, HH mm ss")));
    //System.out.println(date.format(DateTimeFormatter.ofPattern("MMMM ddd yy")));  //java.lang.IllegalArgumentException: Too many pattern letters: d
    System.out.println(date.format(DateTimeFormatter.ofPattern("MMMM dd yy")));
    //System.out.println(time.format(DateTimeFormatter.ofPattern("M d yy, hh:mm:ss")));  //java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: MonthOfYear
    System.out.println(time.format(DateTimeFormatter.ofPattern("hh:mm:ss")));
    
    //format() method can also be invoked on formatter
    System.out.println(DateTimeFormatter.ISO_LOCAL_DATE.format(date));
    System.out.println(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).format(time));
    System.out.println(DateTimeFormatter.ofPattern("yyyy MMM dd HH:mm:ss").format(datetime));
  }
}
OCAJP>javac test.java 
OCAJP>java test
2016-01-01
10:01:01
2016-01-01T10:01:01
1/1/16
10:01:01 AM
10:01:01 AM
Jan 01 2016, 10 01 01
January 01 16
10:01:01
2016-01-01
10:01 AM
2016 Jan 01 10:01:01

Back OCAJP