Java 8 LocalTime example.
LocalTime class is a time without a time-zone in the ISO-8601 calendar system, such as 10:15:30.
LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second.
Time is represented to nanosecond precision.
This class does not store or represent a date or time-zone. Instead, it is a description of the local time as
seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or
time-zone.
This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity
hash code, or synchronization) on instances of LocalTime may have unpredictable results and should be avoided. The equals
method should be used for comparisons.
LocalTime class is immutable and thread-safe.
LocalTimeEx |
package com.java2novice.datetime;
import java.time.LocalTime;
public class LocalTimeEx {
public static void main(String[] args) {
// get current time
LocalTime currTime = LocalTime.now();
System.out.println("current time: "+currTime);
// 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);
// converts no of seconds of the day to time
LocalTime secOfDay = LocalTime.ofSecondOfDay(10160);
System.out.println("seconds of the day: "+secOfDay);
}
}
|
|