HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java LocalTime class

Java LocalTime class

java.time.LocalTime class, introduced in Java 8, represents a local time object without the date or timezone information as hour-minute-second parts. It represents time to nanosecond precision e.g. 09:25:59.123456789

We can use the LocalTime instances where we need to represent a time without any need of the date or timezone reference as seen in a wall clock. For example, we can use LocalTime to represent LocalTime class to capture at which time an employee entered the office building and at which time left.

Note that LocalTime instances are immutable and thread-safe.

1. LocalTime class declaration

public final class LocalTime
	extends Object
	implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable
{
	//class body
}

2. How to create LocalTime in Java

Generally, we will be creating local time instances in two conditions i.e. get current time or create local time for a specified timestamp.

2.1. Get current local time

Use following method to get the current local time.

LocalTime now = LocalTime.now();

2.2. Create specified local time

To create a local time with specific hour, minute and seconds – use below methods.

LocalTime ltObject1 = LocalTime.of(08, 20, 45);	

LocalTime ltObject2 = LocalTime.of(08, 20, 45, 123456789);

LocalTime ltObject3 = LocalTime.parse("08:20");

LocalTime ltObject4 = LocalTime.parse("08:20:45.123456789");

3. How to parse string to LocalTime

The LocalTime class has two overloaded parse() methods to convert time in string to local time instance.

parse(CharSequence text)	//1

parse(CharSequence text, DateTimeFormatter formatter)	//2
  • Use first method if the string contains time in ISO_LOCAL_TIME pattern i.e. [HH]:[mm]:[ss]. This is default pattern of local time in Java.
  • For any other time pattern, we need to use second method where we pass the time as string as well as a formatter which represents the pattern of that time string.
//1 - default time pattern
String time = "08:20:45.123456789";
LocalTime localTimeObj = LocalTime.parse(time);

//2 - specific time pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH.mm.ss.nnn");
String time = "08.20.45.123456789";
LocalTime localTimeObj = LocalTime.parse(time, formatter);

4. How to format LocalTime to string

Use LocalTime.format(DateTimeFormatter) method to format a local time to a desired string representation.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH.mm");

LocalTime today = LocalTime.now();

String timeString = today.format(formatter);	//12.38

5. How to modify local time

LocalTime provides below methods which can be used to get to new local time instance relative to available local time instance.

  • plusHours()
  • plusMinutes()
  • plusSeconds()
  • plusNanos()
  • minusHours()
  • minusMinutes()
  • minusSeconds()
  • minusNanos()
LocalTime now = LocalTime.now();

//3 hours later
LocalTime localTime1 = now.plusHours(3);	

//3 minutes earliar
LocalTime localTime2 = now.minusMinutes(3);

6. More examples

Convert between LocalTime to java.sql.Time

Happy Learning !!

Ref : LocalTime Java Doc

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Date Time Tutorial

  • Java – Date Time APIs
  • Java – Date Parsing
  • Java – Date Formatting
  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – Period
  • Java 8 – DateTimeFormatter
  • Java 8 – TemporalAdjusters
  • Java 8 – TemporalQuery
  • Java 8 – DayOfWeek
  • Java – Date
  • Java – Locale

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces