java LocalTime格式化 HH:mm:ss與hh:mm:ss

洛桐發表於2020-12-03

java LocalTime格式化

public static void main(String[] args) {
        System.out.println(toTimeStr(3487000));
    }

    public static final String TIME_PATTERN = "HH:mm:ss";
    public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern(TIME_PATTERN);

    public static String toTimeStr(long duration) {
        LocalTime localTime = LocalTime.ofSecondOfDay(duration / 1000);
        return TIME_FORMATTER.format(localTime);
    }

執行結果:

00:58:07

將HH:mm:ss換成hh:mm:ss:

public static void main(String[] args) {
        System.out.println(toTimeStr(3487000));
    }

    public static final String TIME_PATTERN = "hh:mm:ss";
    public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern(TIME_PATTERN);

    public static String toTimeStr(long duration) {
        LocalTime localTime = LocalTime.ofSecondOfDay(duration / 1000);
        return TIME_FORMATTER.format(localTime);
    }

執行結果:

12:58:07

相關文章