Java Data Type How to - Get time information








Question

We would like to know how to get time information.

Answer

import java.time.LocalTime;
// w ww.j a  v a  2s .  c o  m
public class Main {

  public static void main(String[] args) {
    LocalTime time = LocalTime.of(15, 30, 23, 234); // 15:30:00
    System.out.println(time.getHour()); // 15
    System.out.println(time.getMinute()); // 30
    System.out.println(time.getSecond()); // 23
    System.out.println(time.getNano()); // 234
    System.out.println(time.toSecondOfDay()); // 55823
    System.out.println(time.toNanoOfDay()); // 55823000000234
  }
}

The code above generates the following result.