Date and Time

Java convert hh mm ss to time | Java 8 LocalTime.of

This post shows how to convert hh mm, hh mm ss, etc to LocalTime, LocalTime in java 8 stores time in the ISO-8601 calendar system without a time-zone

LocalTime.of contains three overloaded methods

public static LocalTime of(int hour, int minute)

Obtains an instance of LocalTime from an hour and a minute.

public static LocalTime of(int hour, int minute, int second)

Obtains an instance of LocalTime from an hour, minute and second.

public static LocalTime of(int hour, int minute, int second, int nanoOfSecond)

Obtains an instance of LocalTime from an hour, minute, second and nanosecond.

 

Example

import java.time.Clock;
import java.time.LocalTime;
import java.time.ZoneId;

public class LocalTimeExample 
{
 public static void main(String[] args) 
 {
  // create time object with values
 LocalTime givenTime = LocalTime.of(2, 30);
 System.out.println("early morning time: "+givenTime);
 
 // time with seconds
 LocalTime timeWithSec = LocalTime.of(2, 30, 28);
 System.out.println("early morning time with seconds: "+timeWithSec);

 // post lunch time
 LocalTime postLunch = LocalTime.of(15, 30, 28);
 System.out.println("post lunch time with seconds: "+postLunch);

 }
}

Output:

early morning time: 02:30
early morning time with seconds: 02:30:28
post lunch time with seconds: 15:30:28