Java将毫秒时长转换为带单位的时间(天,小时,分钟,秒)

 // 毫秒时间戳转换为日、时、分、秒
    public static String timeStampToDhms(long milliseconds) {
        long day = TimeUnit.MILLISECONDS.toDays(milliseconds);
        long hours = TimeUnit.MILLISECONDS.toHours(milliseconds) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(milliseconds));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliseconds));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds));
        StringBuilder sb = new StringBuilder();
        if (day != 0) {
            sb.append(day + "天");
        }
        sb.append(hours + "小时");
        sb.append(minutes + "分");
        sb.append(seconds + "秒");
        return sb.toString();
    }

    public static void main(String[] args) {
        long time = 2131412421;

        System.out.println(timeStampToDhms(time));
    }
 

你可能感兴趣的:(笔记,java,开发语言)