Java Data Type How to - List information about LocalTime








Question

We would like to know how to list information about LocalTime.

Answer

// w w  w . ja  va 2s. c o m
import java.time.Clock;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {

      // the current date
      LocalDate currentDate = LocalDate.now();

      // 2014-02-10
      LocalDate tenthFeb2014 = LocalDate.of(2014, Month.FEBRUARY, 10);

      // months values start at 1 (2014-08-01)
      LocalDate firstAug2014 = LocalDate.of(2014, 8, 1);

      // the 65th day of 2010 (2010-03-06)
      LocalDate sixtyFifthDayOf2010 = LocalDate.ofYearDay(2010, 65);


      // times, e.g. 19:12:30.733

      LocalTime currentTime = LocalTime.now(); // current time
      LocalTime midday = LocalTime.of(12, 0); // 12:00
      LocalTime afterMidday = LocalTime.of(13, 30, 15); // 13:30:15

      // 12345th second of day (03:25:45)
      LocalTime fromSecondsOfDay = LocalTime.ofSecondOfDay(12345);

      // dates with times, e.g. 2014-02-18T19:08:37.950
      LocalDateTime currentDateTime = LocalDateTime.now();

      // 2014-10-02 12:30
      LocalDateTime secondAug2014 = LocalDateTime.of(2014, 10, 2, 12, 30);

      // 2014-12-24 12:00
      LocalDateTime christmas2014 = LocalDateTime.of(2014, Month.DECEMBER, 24, 12, 0);

      // current (local) time in Los Angeles
      LocalTime currentTimeInLosAngeles = LocalTime.now(ZoneId.of("America/Los_Angeles"));

      // current time in UTC time zone
      LocalTime nowInUtc = LocalTime.now(Clock.systemUTC());

      System.out.println("date/time creation: currentDate: " + currentDate);
      System.out.println("date/time creation: tenthFeb2014: " + tenthFeb2014);
      System.out.println("date/time creation: firstAug2014: " + firstAug2014);
      System.out.println("date/time creation: sixtyFifthDayOf2010: " + sixtyFifthDayOf2010);
      System.out.println("date/time creation: currentTime: " + currentTime);
      System.out.println("date/time creation: midday: " + midday);
      System.out.println("date/time creation: afterMidday: " + afterMidday);
      System.out.println("date/time creation: fromSecondsOfDay: " + fromSecondsOfDay);
      System.out.println("date/time creation: currentTimeInLosAngeles: " + currentTimeInLosAngeles);
      System.out.println("date/time creation: currentDateTime: " + currentDateTime);
      System.out.println("date/time creation: secondAug2014: " + secondAug2014);
      System.out.println("date/time creation: christmas2014: " + christmas2014);
    }
}

The code above generates the following result.