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

  1. LocalTime MAX
  2. LocalTime MIDNIGHT
  3. LocalTime MIN

Method

  1. LocalTime adjustInto(Temporal temporal)
  2. LocalTime atDate(LocalDate date)
  3. LocalTime atOffset(ZoneOffset offset)
  4. LocalTime compareTo(LocalTime other)
  5. LocalTime equals(Object obj)
  6. LocalTime format(DateTimeFormatter formatter)
  7. LocalTime from(TemporalAccessor temporal)
  8. LocalTime getHour()
  9. LocalTime getLong(TemporalField field)
  10. LocalTime getMinute()
  11. LocalTime getNano()
  12. LocalTime getSecond()
  13. LocalTime get(TemporalField field)
  14. LocalTime hashCode()
  15. LocalTime isAfter(LocalTime other)
  16. LocalTime isBefore(LocalTime other)
  17. LocalTime isSupported(TemporalField field)
  18. LocalTime isSupported(TemporalUnit unit)
  19. LocalTime minusHours(long hoursToSubtract)
  20. LocalTime minusMinutes(long minutesToSubtract)
  21. LocalTime minusNanos(long nanosToSubtract)
  22. LocalTime minusSeconds(long secondsToSubtract)
  23. LocalTime minus(long amountToSubtract, TemporalUnit unit)
  24. LocalTime minus(TemporalAmount amountToSubtract)
  25. LocalTime NOON
  26. LocalTime now()
  27. LocalTime now(Clock clock)
  28. LocalTime now(ZoneId zone)
  29. LocalTime ofNanoOfDay(long nanoOfDay)
  30. LocalTime ofSecondOfDay(long secondOfDay)
  31. LocalTime of(int hour, int minute)
  32. LocalTime of(int hour, int minute, int second)
  33. LocalTime of(int hour, int minute, int second, int nanoOfSecond)
  34. LocalTime parse(CharSequence text)
  35. LocalTime parse(CharSequence text, DateTimeFormatter formatter)
  36. LocalTime plusHours(long hoursToAdd)
  37. LocalTime plusMinutes(long minutesToAdd)
  38. LocalTime plusNanos(long nanosToAdd)
  39. LocalTime plusSeconds(long secondstoAdd)
  40. LocalTime plus(long amountToAdd, TemporalUnit unit)
  41. LocalTime plus(TemporalAmount amountToAdd)
  42. LocalTime query(TemporalQuery query)
  43. LocalTime range(TemporalField field)
  44. LocalTime toNanoOfDay()
  45. LocalTime toSecondOfDay()
  46. LocalTime toString()
  47. LocalTime truncatedTo(TemporalUnit unit)
  48. LocalTime until(Temporal endExclusive, TemporalUnit unit)
  49. LocalTime withHour(int hour)
  50. LocalTime withMinute(int minute)
  51. LocalTime withNano(int nanoOfSecond)
  52. LocalTime withSecond(int second)
  53. LocalTime with(TemporalAdjuster adjuster)
  54. LocalTime with(TemporalField field, long newValue)