Java LocalTime Class

LocalTimeis an important class in the date-time API introduced in Java 8 (java.timepackage), which is used to represent time without time zone information. It specifically handles the time of day, with precision to the nanosecond.


Features of the LocalTime Class

1. Immutability

LocalTime is an immutable class. All modification operations return a new instance, and the original object remains unchanged.

2. No Time Zone Information

LocalTime does not contain time zone information; it only represents local time.

3. Time Precision

Supports time precision from hours to nanoseconds (HH:mm:ss.nnnnnnnnn).


Creating LocalTime Objects

1. Getting the Current Time

LocalTime currentTime = LocalTime.now();
System.out.println("当前时间: " + currentTime);

2. Creating Using the of() Method

Example

// Create 14:30:00
LocalTime time1 = LocalTime.of(14, 30);

// Create 09:15:30
LocalTime time2 = LocalTime.of(9, 15, 30);

// Create 10:20:30.500 (500 milliseconds)
LocalTime time3 = LocalTime.of(10, 20, 30, 500000000);

3. Parsing Strings

LocalTime parsedTime = LocalTime.parse("08:45"); 
System.out.println("解析的时间: " + parsedTime);

Common Methods

1. Getting Time Components

Example

LocalTime time = LocalTime.of(15, 45, 20, 123456789);

<p>int hour = time.getHour();       // 15
int minute = time.getMinute();   // 45
int second = time.getSecond();   // 20
int nano = time.getNano();       // 123456789

2. Time Comparison

Example

LocalTime time1 = LocalTime.of(10, 30);
LocalTime time2 = LocalTime.of(11, 15);

boolean isBefore = time1.isBefore(time2);  // true
boolean isAfter = time1.isAfter(time2);    // false
boolean isEqual = time1.equals(time2);     // false

3. Time Arithmetic

Example

LocalTime time = LocalTime.of(9, 0);

// Add 2 hours
LocalTime plusHours = time.plusHours(2); // 11:00

// Subtract 30 minutes
LocalTime minusMinutes = time.minusMinutes(30); // 08:30

// Add 1 hour and 15 minutes
LocalTime plusDuration = time.plus(Duration.ofMinutes(75)); // 10:15

4. Time Adjustment

Example

LocalTime time = LocalTime.of(14, 30);

// Set the hour to 10
LocalTime withHour = time.withHour(10);  // 10:30

// Set the minute to 45
LocalTime withMinute = time.withMinute(45);  // 14:45

Formatting and Parsing

1. Formatting Time

Example

java LocalTime time = LocalTime.of(16, 45, 30);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String formattedTime = time.format(formatter); // "16:45:30"

2. Parsing Time Strings

Example

String timeString = "08:15:45 AM";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
LocalTime parsedTime = LocalTime.parse(timeString, formatter);

Practical Application Examples

1. Calculating the Interval Between Two Time Points

Example

LocalTime start = LocalTime.of(9, 0);
LocalTime end = LocalTime.of(17, 30);
Duration duration = Duration.between(start, end);
System.out.println("Working hours: " + duration.toHours() + " hours"); // 8 hours

2. Checking Whether It Is Within Business Hours

Example

LocalTime current = LocalTime.now();
LocalTime opening = LocalTime.of(8, 0);
LocalTime closing = LocalTime.of(22, 0);

if (current.isAfter(opening) && current.isBefore(closing)) {
    System.out.println("The store is open");
} else {
    System.out.println("The store is closed");
}

3. Creating a Time Range

Example

LocalTime start = LocalTime.of(8, 0);
LocalTime end = start.plusHours(8); // 16:00
System.out.println("Working hours: " + start + " to " + end);

Summary

LocalTimeis a core class in the Java 8 date-time API for handling local time. It provides rich methods for creating, manipulating, and formatting time. Due to its immutability and thread safety,LocalTimeit is very suitable for use in multi-threaded environments. MasteringLocalTimethe use of this class can greatly simplify time-related programming tasks.

Other Extensions