Java 8 - LocalTime example

Posted on May 23, 2017


The java.time.LocalTime class is an immutable class which represents a time without time-zone information such as ‘21:23:38.555’.

This class does not store and represent a date or time-zone. Instead, it is a description of the time which can be used for employee attendance (in/out time) tracking.

The following examples are provided to demonstrate the usage of the LocalTime in java program.

How to create a LocalDate in Java 8

You can use the LocalTime.now() and LocalTime.of() methods to create the current time and specific time object respectively.

package com.boraji.tutorial.datetime;

import java.time.LocalTime;

public class LocalTimeExample {
   public static void main(String[] args) {

      // Current Time
      LocalTime localTime = LocalTime.now();
      System.out.println(localTime);

      // Specific Time
      LocalTime localTime2 = LocalTime.of(4, 30, 45);
      System.out.println(localTime2);
   }
}

Output

21:43:09.750
04:30:45

How to get Hour, Minute, Second from LocalTime

You can use the getHour()getMinute() and getSecond() methods of the LocalTime class to get hour, minute and second respectively.

package com.boraji.tutorial.datetime;

import java.time.LocalTime;

public class LocalTimeExample {
   public static void main(String[] args) {

      LocalTime localTime = LocalTime.now();
      System.out.println("Hour : " + localTime.getHour());
      System.out.println("Minute : " + localTime.getMinute());
      System.out.println("Second : " + localTime.getSecond());
   }
}

Output

Hour : 21
Minute : 48
Second : 40

How to add or subtract hours, minutes and seconds to LocalTime

You can use the plus and minus methods of the LocalTime class to add or subtract hours, minutes etc.

package com.boraji.tutorial.datetime;

import java.time.LocalTime;

public class LocalTimeExample {
   public static void main(String[] args) {

      LocalTime localTime = LocalTime.now();
      System.out.println("Current Time : " + localTime);

      // LocalTime's plus methods
      System.out.println("Addition of Hours : " + localTime.plusHours(2));
      System.out.println("Addition of Minutes : " + localTime.plusMinutes(30));
      System.out.println("Addition of Seconds : " + localTime.plusSeconds(20));
      System.out.println("Addition of Nanos : " + localTime.plusNanos(60000));

      // LocalTime's minus methods
      System.out.println("Subtraction  of Hours : " + localTime.minusHours(2));
      System.out.println("Subtraction  of Minutes : " + localTime.minusMinutes(30));
      System.out.println("Subtraction  of Seconds : " + localTime.minusSeconds(20));
      System.out.println("Subtraction  of Nanos : " + localTime.minusNanos(60000));

   }
}

Output

Current Time : 21:55:54.672
Addition of Hours : 23:55:54.672
Addition of Minutes : 22:25:54.672
Addition of Seconds : 21:56:14.672
Addition of Nanos : 21:55:54.672060
Subtraction  of Hours : 19:55:54.672
Subtraction  of Minutes : 21:25:54.672
Subtraction  of Seconds : 21:55:34.672
Subtraction  of Nanos : 21:55:54.671940

How to compare LocalTime objects in Java

You can use the compareTo()isAfter() and isBefore() methods of the LocalTime class to compare the LocalTime objects.

package com.boraji.tutorial.datetime;

import java.time.LocalTime;

public class LocalTimeExample {
   public static void main(String[] args) {

      LocalTime localTime1 = LocalTime.of(9, 10, 50);
      LocalTime localTime2 = LocalTime.of(9, 10, 50);
      LocalTime localTime3 = LocalTime.of(11, 45, 20);

      // compareTo() example
      if (localTime1.compareTo(localTime2) == 0) {
         System.out.println("localDate1 and localDate2 are equal");
      } else {
         System.out.println("localDate1 and localDate2 are not equal");
      }

      // isBefore() example
      if (localTime2.isBefore(localTime3)) {
         System.out.println("localTime2 comes before localTime3");
      }

      // isAfter() example
      if (localTime3.isAfter(localTime1)) {
         System.out.println("localTime3 comes after localTime1");
      }
   }
}

Output

localDate1 and localDate2 are equal
localTime2 comes before localTime3
localTime3 comes after localTime1

 

How to convert String to LocalTime in java

You can use the parse() method of the LocalTime class to convert the String to LocalTime.

package com.boraji.tutorial.datetime;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class LocalTimeExample {
   public static void main(String[] args) {

      LocalTime isoTime = LocalTime.parse("10:15:30",
            DateTimeFormatter.ISO_LOCAL_TIME);
      System.out.println(isoTime);

      // hour-of-day (0-23)
      LocalTime localTime = LocalTime.parse("22:45:30",
            DateTimeFormatter.ofPattern("HH:mm:ss"));
      System.out.println(localTime);

      // clock-hour-of-am-pm (1-24)
      LocalTime localTime2 = LocalTime.parse("22:45:30",
            DateTimeFormatter.ofPattern("kk:mm:ss"));
      System.out.println(localTime2);

      // clock-hour-of-am-pm (1-12)
      LocalTime localTime3 = LocalTime.parse("10:45:30 PM",
            DateTimeFormatter.ofPattern("hh:mm:ss a"));
      System.out.println(localTime3);

      // hour-of-am-pm (0-11)
      LocalTime localTime4 = LocalTime.parse("10:45:30 AM",
            DateTimeFormatter.ofPattern("KK:mm:ss a"));
      System.out.println(localTime4);

   }
}

Output

10:15:30
22:45:30
22:45:30
22:45:30
10:45:30

How to convert LocalTime to String in java

You can use the format() method of the LocalTime class to convert the LocalTime to String.

package com.boraji.tutorial.datetime;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class LocalTimeExample {
   public static void main(String[] args) {

      LocalTime localTime = LocalTime.now();

      // ISO Format
      DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_LOCAL_TIME;
      System.out.println(localTime.format(timeFormatter));

      // hour-of-day (0-23)
      DateTimeFormatter timeFormatter1 = DateTimeFormatter
            .ofPattern("HH:mm:ss");
      System.out.println(localTime.format(timeFormatter1));

      // clock-hour-of-am-pm (1-24)
      DateTimeFormatter timeFormatter2 = DateTimeFormatter
            .ofPattern("kk:mm:ss");
      System.out.println(localTime.format(timeFormatter2));

      // clock-hour-of-am-pm (1-12)
      DateTimeFormatter timeFormatter3 = DateTimeFormatter
            .ofPattern("hh:mm:ss a");
      System.out.println(localTime.format(timeFormatter3));

      // hour-of-am-pm (0-11)
      DateTimeFormatter timeFormatter4 = DateTimeFormatter
            .ofPattern("KK:mm:ss a");
      System.out.println(localTime.format(timeFormatter4));

   }
}

Output

22:47:27.905
22:47:27
22:47:27
10:47:27 PM
10:47:27 PM