Java Date Time - LocalTime Example
Example
The following code shows how to
get hours and minutes between local time in different time zone.
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
/*from w ww. j a va2s.co m*/
public class Main {
public static void main(String... args) {
ZoneId zone1 = ZoneId.of("Europe/Berlin");
ZoneId zone2 = ZoneId.of("Brazil/East");
LocalTime now1 = LocalTime.now(zone1);
LocalTime now2 = LocalTime.now(zone2);
System.out.println(now1);
System.out.println(now2);
long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
System.out.println(hoursBetween);
System.out.println(minutesBetween);
}
}
The code above generates the following result.
Example 2
The following code shows how to
parse string to LocalTime with short German format.
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
//from w ww. j av a 2s . c om
public class Main {
public static void main(String... args) {
DateTimeFormatter germanFormatter =
DateTimeFormatter
.ofLocalizedTime(FormatStyle.SHORT)
.withLocale(Locale.GERMAN);
LocalTime leetTime = LocalTime.parse("13:37", germanFormatter);
System.out.println(leetTime);
}
}
The code above generates the following result.
Field
- LocalTime MAX
- LocalTime MIDNIGHT
- LocalTime MIN
Method
- LocalTime adjustInto(Temporal temporal)
- LocalTime atDate(LocalDate date)
- LocalTime atOffset(ZoneOffset offset)
- LocalTime compareTo(LocalTime other)
- LocalTime equals(Object obj)
- LocalTime format(DateTimeFormatter formatter)
- LocalTime from(TemporalAccessor temporal)
- LocalTime getHour()
- LocalTime getLong(TemporalField field)
- LocalTime getMinute()
- LocalTime getNano()
- LocalTime getSecond()
- LocalTime get(TemporalField field)
- LocalTime hashCode()
- LocalTime isAfter(LocalTime other)
- LocalTime isBefore(LocalTime other)
- LocalTime isSupported(TemporalField field)
- LocalTime isSupported(TemporalUnit unit)
- LocalTime minusHours(long hoursToSubtract)
- LocalTime minusMinutes(long minutesToSubtract)
- LocalTime minusNanos(long nanosToSubtract)
- LocalTime minusSeconds(long secondsToSubtract)
- LocalTime minus(long amountToSubtract, TemporalUnit unit)
- LocalTime minus(TemporalAmount amountToSubtract)
- LocalTime NOON
- LocalTime now()
- LocalTime now(Clock clock)
- LocalTime now(ZoneId zone)
- LocalTime ofNanoOfDay(long nanoOfDay)
- LocalTime ofSecondOfDay(long secondOfDay)
- LocalTime of(int hour, int minute)
- LocalTime of(int hour, int minute, int second)
- LocalTime of(int hour, int minute, int second, int nanoOfSecond)
- LocalTime parse(CharSequence text)
- LocalTime parse(CharSequence text, DateTimeFormatter formatter)
- LocalTime plusHours(long hoursToAdd)
- LocalTime plusMinutes(long minutesToAdd)
- LocalTime plusNanos(long nanosToAdd)
- LocalTime plusSeconds(long secondstoAdd)
- LocalTime plus(long amountToAdd, TemporalUnit unit)
- LocalTime plus(TemporalAmount amountToAdd)
- LocalTime query(TemporalQuery query)
- LocalTime range(TemporalField field)
- LocalTime toNanoOfDay()
- LocalTime toSecondOfDay()
- LocalTime toString()
- LocalTime truncatedTo(TemporalUnit unit)
- LocalTime until(Temporal endExclusive, TemporalUnit unit)
- LocalTime withHour(int hour)
- LocalTime withMinute(int minute)
- LocalTime withNano(int nanoOfSecond)
- LocalTime withSecond(int second)
- LocalTime with(TemporalAdjuster adjuster)
- LocalTime with(TemporalField field, long newValue)