Formatting local time in java

后端 未结 4 1815
盖世英雄少女心
盖世英雄少女心 2021-01-02 00:47

I\'m creating my JavaFX application and I need to use time label every time new list cell is created. I need to put the string with current time in HH:MM format

4条回答
  •  难免孤独
    2021-01-02 01:27

    You can create a LocalTime instance in several ways. The first way is to create a LocalTime instance that represents the exact time of now. Here is how that looks:

    LocalTime localTime = LocalTime.now();
    

    Another way to create a LocalTime object is to create it from a specific amount of hours, minutes, seconds and nanoseconds. Here is how that looks:

    LocalTime localTime2 = LocalTime.of(21, 30, 59, 11001);
    

    There are also other versions of the of() method that only takes hours and minutes, or hours, minutes and seconds as parameters.

    You can access the hours, minutes, seconds and nanosecond of a LocalTime object using these methods:

    • getHour()
    • getMinute()
    • getSecond()
    • getNano()

    check this out, it helped me alot- http://tutorials.jenkov.com/java-date-time/localtime.html

提交回复
热议问题