Friday, September 29, 2017

Java 8 Date Time API: LocalTime

With Java 8 Oracle introduced new Date and Time APIs which address the shortcomings of the existing java.util.Date and java.util.Calendar. In this article we will take a look at the Java 8 Date and Time API with few examples of the new LocalTime class introduced by Java 8.

Improvements to Existing Calendar and Date Classes

Thread Safety: The existing API was not designed with concurrency in mind. This meant developers had to come up with custom solutions to handle thread safety. The Java 8 Date/Time API ensures thread safety by making all the new classes immutable. Domain-driven design: The Java 8 date/time API has clear domain boundaries, and defines classes with specific use cases. Support for non-standard Calendars: The Java 8 Date/Time API provides support for non-standard calendaring systems (Japanese calendar for example) without affecting the standard calendar implementation.


java.time.LocalTime, like LocalDate, represents the Time, without the Date. The following examples show show a few ways to use the LocalTime class
  • Create an instance of LocalTime for current time: The instance representing current time can be obtained using the now() method
    LocalTime localTime =;
  • Create an instance of LocaTime representing a specific time: Again, like LocalDate, there are two variations in which you can instantiate LocalTime to represent a specific time:
    • The LocalTime.of method takes the distinct elements of the time as parameters (hours, minutes, seconds etc.). The following variants are available
      static LocalTime of(int hour, int minute)
      static LocalTime of(int hour, int minute, int second)
      static LocalTime of(int hour, int minute, int second, int nanoOfSecond)
      The following example shows the use of this method
      LocalTime specificTime1 = LocalTime.of(21, 30); // 9:30 PM
    • The second method is LocalTime.parse which again has two variations
      • The default format is HH:mm:ss.nnn. The first variant of parse, takes just a string representation of the date and attempts to parse the date by the default format. We can omit the trailing sections of the time, and still be able to parse the time.
        LocalTime specificTime2 = LocalTime.parse("21:30:33.435"); 
        System.out.println("With LocalTime.parse, and default format : " + specificTime2);
        LocalTime specificTime3 = LocalTime.parse("21:30:33"); 
        System.out.println("With LocalTime.parse, and default format and remove nanoseconds : " + specificTime3);
      • Custom Time Format: In this variation, we can use custom formatting using DateTimeFormatter.ofPattern. In the following example, we will use the parse method to parse a simple time format ("09:30 PM")
        DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("hh:mm a");
        LocalTime specificTime4 = LocalTime.parse("09:30 PM", dateTimeFormat); 
        System.out.println("With LocalTime.parse, and custom format : " + specificTime4); 
  • Get parts of the time: LocalTime also provides a few utility method to extract part of the time and get more information about the date
    System.out.println("Hour of the day : " + localTime.getHour());
    System.out.println("Minute of the hour : " + localTime.getMinute());
  • Add or Subtract with LocalTime: LocalTime class provides utility methods to add or subtract hours/minutes/seconds (represented by java.time.temporal.TemporalUnit interface). The following examples illustrate the use of these operation on LocalTime.
    //Subtract Hours
    System.out.println("3 hours before now is : " + localTime.minus(3, ChronoUnit.HOURS));
    //Subtract Minutes
    System.out.println("3 minutes before now is : " + localTime.minus(3, ChronoUnit.MINUTES));
    //Add Hours
    System.out.println("3 hours after now is : " +, ChronoUnit.HOURS));
    //Add Minutes
    System.out.println("3 minutes after now is : " +, ChronoUnit.MINUTES));
  • Compare two LocalTimes: The isBefore and isAfter offer a way to compare two LocalTime objects
    System.out.println("Is the time after 11:26? " + localTime.isAfter(LocalTime.parse("11:26")));
    System.out.println("Is the time before 11:30? " + localTime.isBefore(LocalTime.parse("11:30")));
  • Time between two LocalTimes: The until() method provides a way to calculate the number of hours/minutes etc between two LocalTimes. This method takes in the end time and TemporalUnit as a parameter, and can be used to calculate the number hours, minutes etc. between the start time and end time.
    System.out.println("Number of hours from now to 11:30: " + localTime.until(LocalTime.parse("11:30"), ChronoUnit.HOURS));
    System.out.println("Number of minutes from now to 11:30: " + localTime.until(LocalTime.parse("11:30"), ChronoUnit.MINUTES));

1 comment:

Popular Posts