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

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
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页