Formatting local time in java

后端 未结 4 1816
盖世英雄少女心
盖世英雄少女心 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条回答
  •  萌比男神i
    2021-01-02 01:23

    It's probably better to use Java 8 types (java.time) in a new application. You can first create a DateTimeFormatter:

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
    

    And then get the current time and format it:

    Label timeLabel = new Label(LocalTime.now().format(dtf));
    

提交回复
热议问题