Formatting local time in java

后端 未结 4 1832
盖世英雄少女心
盖世英雄少女心 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:18

    LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm"))
    

    (this will use the default locale, see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ofPattern-java.lang.String- for more).

    But better practice would be to put the formatter into a static final field. This way it's only created once instead of every time the line is exectuted. It nearly certainly doesn't really matter for this application, but it's better to use good habits from the beginning.

提交回复
热议问题