Home | FAQ | Contact me

Java LocalDate LocalTime and LocalDateTime

How to make quick use of LocalDate to deal with common date operations, like birthdates, in Java.

package com.windofkeltia.leap;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import org.junit.runners.MethodSorters;

@FixMethodOrder( MethodSorters.JVM )
public class LocalDateTimeFunTest
{
  @Rule   public TestName name = new TestName();
  @Before public void setUp()
  {
    String testName  = name.getMethodName();
    int    PAD       = 100;
    int    nameWidth = testName.length();
    System.out.print( "Test: " + testName + " " );
    PAD -= nameWidth;
    while( PAD-- > 0 )
      System.out.print( "-" );
    System.out.println();
  }

  private static final boolean VERBOSE = true;

  @Test
  public void testYyyyMmDdIso()
  {
    String            date      = "19550118";
    DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
    LocalDate         timestamp = LocalDate.parse( date, formatter );
    if( VERBOSE )
      System.out.println( "  " + timestamp );
  }

  @Test
  public void testYyyyMmDd()
  {
    String            date      = "1955-01-18";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyy-MM-dd" );
    LocalDate         timestamp = LocalDate.parse( date, formatter );
    if( VERBOSE )
      System.out.println( "  " + timestamp );
  }

  @Test
  public void testYyyyMmDdHhMm()
  {
    String            date      = "1955-01-18 04:32";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm" );
    LocalDateTime     timestamp = LocalDateTime.parse( date, formatter );
    if( VERBOSE )
      System.out.println( "  " + timestamp );
  }

  @Test
  public void testJumble1()
  {
    String            date      = "Tuesday, 16 August 2016, 12:10:56 PM";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "EEEE, d MMMM yyyy, HH:mm:ss a" );
    LocalDateTime     timestamp = LocalDateTime.parse( date, formatter );
    if( VERBOSE )
      System.out.println( "  " + timestamp );
  }

  @Test
  public void testJumble2()
  {
    String            date      = "Tuesday, Aug 16, 2016 12:10:56 PM";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "EEEE, MMM d, yyyy HH:mm:ss a" );
    LocalDateTime     timestamp = LocalDateTime.parse( date, formatter );
    if( VERBOSE )
      System.out.println( "  " + timestamp );
  }

  @Test
  public void testWithTimeZone1()
  {
    String            date      = "2016-08-16T15:23:01Z";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "EEEE, MMM d, yyyy HH:mm:ss a" );
    Instant           instant   = Instant.parse( date );
    LocalDateTime     timestamp = LocalDateTime.ofInstant( instant, ZoneId.of( ZoneOffset.UTC.getId() ) );
    if( VERBOSE )
    {
      System.out.println( "  " + timestamp );
      ZonedDateTime zonedDateTime = instant.atZone( ZoneId.of( "Asia/Tokyo" ) );
      System.out.println( "  " + zonedDateTime );
      ZonedDateTime zonedDateTime2 = instant.atZone( ZoneId.of( "America/Denver" ) );
      System.out.println( "  " + zonedDateTime2 );
      ZonedDateTime zonedDateTime3 = instant.atZone( ZoneId.of( "Europe/Athens" ) );
      System.out.println( "  " + zonedDateTime3 );
    }
  }

  @Test
  public void testWithTimeZone2()
  {
    String            date      = "2016-08-16T10:15:30+08:00";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "EEEE, MMM d, yyyy HH:mm:ss a" );
    ZonedDateTime     timestamp = ZonedDateTime.parse( date, DateTimeFormatter.ISO_DATE_TIME );
    if( VERBOSE )
    {
      System.out.println( "        Local date: " + timestamp.toLocalDate() );
      System.out.println( "  Time in timezone: " + timestamp );
      System.out.println( "          Timezone: " + timestamp.getZone() );
    }
  }
}

Output:

Test: testYyyyMmDdIso ----------------------------------------------------------------------------
  1955-01-18
Test: testYyyyMmDd -------------------------------------------------------------------------------
  1955-01-18
Test: testYyyyMmDdHhMm ---------------------------------------------------------------------------
  1955-01-18T04:32
Test: testJumble1 --------------------------------------------------------------------------------
  2016-08-16T12:10:56
Test: testJumble2 --------------------------------------------------------------------------------
  2016-08-16T12:10:56
Test: testWithTimeZone1 --------------------------------------------------------------------------
  2016-08-16T15:23:01
  2016-08-17T00:23:01+09:00[Asia/Tokyo]
  2016-08-16T09:23:01-06:00[America/Denver]
  2016-08-16T18:23:01+03:00[Europe/Athens]
Test: testWithTimeZone2 --------------------------------------------------------------------------
        Local date: 2016-08-16
  Time in timezone: 2016-08-16T10:15:30+08:00
        Timezone: +08:00