YouTip LogoYouTip

Java Localtime Class

`LocalTime` is an important class introduced in Java 8's Date and Time API (`java.time` package), used to represent time without timezone information. It specifically handles time of day, precise to nanosecond level. * * * ## Features of LocalTime Class ### 1. Immutability LocalTime is an immutable class, all modification operations return new instances, the original object remains unchanged. ### 2. No Timezone Information LocalTime does not contain timezone 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 Current Time LocalTime currentTime = LocalTime.now();System.out.println("Current Time: " + currentTime); ### 2. Using 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 String LocalTime parsedTime = LocalTime.parse("08:45"); System.out.println("Parsed Time: " + parsedTime); * * * ## Common Methods ### 1. Getting Time Components ## Example LocalTime time = LocalTime.of(15, 45, 20, 123456789);

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 15 minutes LocalTime plusDuration = time.plus(Duration.ofMinutes(75));// 10:15 ### 4. Time Adjustment ## Example LocalTime time = LocalTime.of(14, 30); // Set hour to 10 LocalTime withHour = time.withHour(10);// 10:30 // Set 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 String ## 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 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 If 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("Store is open"); }else{ System.out.println("Store is closed"); } ### 3. Creating 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 `LocalTime` class is the core class in Java 8's Date and Time API for handling local time. It provides rich methods for creating, manipulating, and formatting time. Due to its immutability and thread-safety, `LocalTime` is very suitable for use in multi-threaded environments. Mastering the use of the `LocalTime` class can greatly simplify time-related programming tasks.

← Java Instant ClassJava Localdatetime Class β†’