1.Math、System、Runtime

常用API 2_时间戳

public class test {
    public static void main(String[] args) {
        // 1. 绝对值
        int absInt = Math.abs(-10);
        double absDouble = Math.abs(-10.5);
        System.out.println("绝对值 (int): " + absInt);       // 输出 10
        System.out.println("绝对值 (double): " + absDouble); // 输出 10.5

        // 2. 取整
        double ceilValue = Math.ceil(10.1);
        double floorValue = Math.floor(10.9);
        long roundValue = Math.round(10.5);
        System.out.println("向上取整: " + ceilValue);  // 输出 11.0
        System.out.println("向下取整: " + floorValue); // 输出 10.0
        System.out.println("四舍五入: " + roundValue); // 输出 11

        // 3. 最值
        int maxValue = Math.max(10, 20);
        int minValue = Math.min(10, 20);
        System.out.println("较大值: " + maxValue); // 输出 20
        System.out.println("较小值: " + minValue); // 输出 10

        // 4. 幂次方
        double powValue = Math.pow(2, 3);
        System.out.println("2 的 3 次方: " + powValue); // 输出 8.0

        // 5. 随机数
        double randomValue = Math.random();
        System.out.println("随机数: " + randomValue); // 输出 0.0 到 1.0 之间的随机值
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

(2)System

public class test {
    public static void main(String[] args) {
        System.out.println("开始执行...");

        // 结束当前运行的 Java 虚拟机,不要使用
        System.exit(0);

        // 这行代码不会执行
        System.out.println("这行代码不会执行...");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
public class test {
    public static void main(String[] args) {
        // 记录当前时间(开始时间)
        long time = System.currentTimeMillis();
        System.out.println("开始时间(毫秒):" + time);

        // 模拟一些耗时操作
        for (int i = 0; i < 1000000; i++) {
            System.out.println("输出了:" + i);
        }

        // 记录当前时间(结束时间)
        long time2 = System.currentTimeMillis();
        System.out.println("结束时间(毫秒):" + time2);

        // 计算并打印程序执行时间
        System.out.println("程序执行时间:" + (time2 - time) / 1000.0 + " 秒");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

(3)Runtime  单例类,调用getRuntime

常用API 2_时间戳_02

package com.lzk.test;

public class test {
    public static void main(String[] args) {
        // 获取与当前Java应用程序相关的运行时对象
        Runtime runtime = Runtime.getRuntime();

        // 可用处理器数
        int processors = runtime.availableProcessors();
        System.out.println("可用处理器数: " + processors);

        // Java虚拟机中的内存总量
        long totalMemory = runtime.totalMemory();
        System.out.println("内存总量: " + totalMemory /1024/1024+ " MB");

        // Java虚拟机中的可用内存
        long freeMemory = runtime.freeMemory();
        System.out.println("可用内存: " + freeMemory /1024/1024+ " MB");

        // 启动一个新进程(在此例中,不启动进程)
        try {
            Process process = runtime.exec("notepad");//需要将软件配置到环境变量
            // 等待进程结束
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();// 异常处理
        }

        // 终止当前运行的Java虚拟机
        // 注意:这会停止程序运行,因此在此例中注释掉
        // runtime.exit(0);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

2.BigDecimal

用于解决浮点型运算时,出现结果失真的问题,因为浮点型加减乘除的时候,对于小数部分不会精确的表示,会有结果的失真。

常用API 2_Math_03

public class test {
    public static void main(String[] args) {
        // 创建 BigDecimal 对象
        BigDecimal bd1 = new BigDecimal("123.456");
        BigDecimal bd2 = BigDecimal.valueOf(234.567);

        // 加法
        BigDecimal sum = bd1.add(bd2);
        System.out.println("加法结果: " + sum); // 输出 "加法结果: 358.023"

        // 减法
        BigDecimal difference = bd1.subtract(bd2);
        System.out.println("减法结果: " + difference); // 输出 "减法结果: -111.111"

        // 乘法
        BigDecimal product = bd1.multiply(bd2);
        System.out.println("乘法结果: " + product); // 输出 "乘法结果: 28937.868952"

        // 除法,以防出现三分之一问题,除不尽导致出错。
        BigDecimal quotient = bd1.divide(bd2, 5, RoundingMode.HALF_UP);
        System.out.println("除法结果: " + quotient); // 输出 "除法结果: 0.52633"

        // 将 BigDecimal 转换为 double
        double doubleValue = bd1.doubleValue();
        System.out.println("double 值: " + doubleValue); // 输出 "double 值: 123.456"
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

3.JDK8之前的日期时间

(1)Date  毫秒值,不直观

常用API 2_System_04

public class test {
    public static void main(String[] args) {
        // 1. 创建一个 Date 对象,代表系统当前时间信息
        Date d = new Date();
        System.out.println("当前时间: " + d);

        // 2. 获取时间戳值(从1970年1月1日 00:00:00 GMT到当前时间的毫秒数)
        long time = d.getTime();
        System.out.println("时间戳: " + time);

        // 3. 把时间戳值转换成日期对象 (2秒后的时间)
        time += 2 * 1000; // 加 2 秒
        Date d2 = new Date(time);
        System.out.println("2 秒后的时间: " + d2);

        // 4. 直接使用 Date 对象的 setTime 方法进行修改
        Date d3 = new Date();
        d3.setTime(time);
        System.out.println("使用 setTime 修改后的时间: " + d3);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

(2)Simpledateformat

常用API 2_System_05

常用API 2_Math_06

public class test {
    public static void main(String[] args) throws ParseException {
        // 创建一个 SimpleDateFormat 对象,指定日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 获取当前时间
        Date now = new Date();
        System.out.println("当前时间: " + now);

        // 格式化当前时间
        String formattedDate = sdf.format(now);
        System.out.println("格式化当前时间: " + formattedDate);

        // 将时间戳转换为日期对象
        long time = now.getTime(); // 获取当前时间的时间戳
        Date dateFromTimestamp = new Date(time);

        // 格式化时间戳
        String formattedTimestamp = sdf.format(dateFromTimestamp);
        System.out.println("格式化时间戳: " + formattedTimestamp);
        
        
        
        // 目标:掌握 SimpleDateFormat 解析字符串时间成为日期对象
        String dateStr = "2022-12-12 12:12:11";

        // 1. 创建 SimpleDateFormat 对象,指定的时间格式必须与解析的时间格式一样,否则程序会出 bug。
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 2. 使用 SimpleDateFormat 解析字符串时间

        Date d2 = sdf2.parse(dateStr);
        System.out.println(d2);

    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.

常用API 2_时间戳_07

案例:秒杀活动

秒杀开始时间2024.11.11  0:0:0

结束时间2024.11.11. 0:10:0

public class test {
    public static void main(String[] args) {
        // 1. 把开始时间、结束时间、小贾下单时间、小皮下单时间拿到
        String start = "2023年11月1日 0:0:0";
        String end = "2023年11月1日 1:0:0";
        String xj = "2023年11月1日 0:01:18";
        String xp = "2023年11月1日 0:10:57";

        // 2. 把字符串的时间解析成日期对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        try {
            Date startDt = sdf.parse(start);
            Date endDt = sdf.parse(end);
            Date xjDt = sdf.parse(xj);
            Date xpDt = sdf.parse(xp);

            // 3. 开始时间的毫秒和小贾是否成功下单。把日期对象转换时间戳的毫秒值判断
            long startTime = startDt.getTime();
            long endTime = endDt.getTime();
            long xjTime = xjDt.getTime();
            long xpTime = xpDt.getTime();

            if (xjTime >= startTime && xjTime <= endTime) {
                System.out.println("小贾您秒杀成功");
            } else {
                System.out.println("小贾您秒杀失败");
            }

            // 判断小皮是否秒杀成功
            if (xpTime >= startTime && xpTime <= endTime) {
                System.out.println("小皮您秒杀成功");
            } else {
                System.out.println("小皮您秒杀失败");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

(3)Calendar

下面这个需求太麻烦

常用API 2_Math_08

Calendar对应的时此刻时间对应的日历,通过它可以直接获取或者修改年月日时秒分

常用API 2_Math_09

public class test {
    public static void main(String[] args) {
        // 获取当前日历对象的实例
        Calendar cal = Calendar.getInstance();

        // 获取当前年份、月份、日期
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1; // 月份从 0 开始,因此加 1
        int day = cal.get(Calendar.DAY_OF_MONTH);
        System.out.println("当前日期: " + year + "年" + month + "月" + day + "日");

        // 获取当前时间
        Date date = cal.getTime();
        System.out.println("当前时间: " + date);

        // 获取当前时间的毫秒值
        long timeInMillis = cal.getTimeInMillis();
        System.out.println("当前时间的毫秒值: " + timeInMillis);

        // 修改日历中的某个字段的值,例如设置年份为2022年
        cal.set(Calendar.YEAR, 2022);
        System.out.println("修改后的年份: " + cal.get(Calendar.YEAR));

        // 为某个字段增加或减少指定的值,例如增加 5 天
        cal.add(Calendar.DAY_OF_MONTH, 5);
        System.out.println("增加 5 天后的日期: " + cal.getTime());
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

常用API 2_System_10

4.JDK8之后的时间

常用API 2_时间戳_11

(1)LocalDate、LocalTime、LocalDateTime代替之前的Calendar

LocalDate:代表本地日期(年、月、日、星期)。

LocalTime:代表本地时间(时、分、秒、纳米)。

LocalDateTime:二者的结合。

三者获取对象是调用now(),非常类似。

下面以LocalDate为例

public class test {
    public static void main(String[] args) {
        // 0. 获取本地日期对象
        LocalDate ld = LocalDate.now(); // 年 月 日
        System.out.println(ld);

        // 1. 获取日期对象中的信息
        int year = ld.getYear(); // 年
        int month = ld.getMonthValue(); // 月(1-12)
        int day = ld.getDayOfMonth(); // 日
        int dayOfYear = ld.getDayOfYear(); // 一年中的第几天
        int dayOfWeek = ld.getDayOfWeek().getValue(); // 星期几(1 = Monday, ..., 7 = Sunday)

        System.out.println("年: " + year);
        System.out.println("月: " + month);
        System.out.println("日: " + day);
        System.out.println("一年中的第几天: " + dayOfYear);
        System.out.println("星期几: " + dayOfWeek);

        // 2. 直接修改某项信息
        LocalDate newYearDate = ld.withYear(2025); // 修改年为 2025
        LocalDate newMonthDate = ld.withMonth(12); // 修改月为 12
        LocalDate newDayOfMonthDate = ld.withDayOfMonth(25); // 修改日为 25

        System.out.println("修改后的年份: " + newYearDate);
        System.out.println("修改后的月份: " + newMonthDate);
        System.out.println("修改后的日期: " + newDayOfMonthDate);

        // 3. 把某个信息加多少,这是新返回一个对象,不会改变初始时间
        LocalDate nextYear = ld.plusYears(1); // 加 1 年
        LocalDate nextMonth = ld.plusMonths(1); // 加 1 个月
        LocalDate nextDay = ld.plusDays(1); // 加 1 天

        System.out.println("下一年: " + nextYear);
        System.out.println("下一个月: " + nextMonth);
        System.out.println("下一天: " + nextDay);

        // 4. 把某个信息减多少
        LocalDate previousYear = ld.minusYears(1); // 减 1 年
        LocalDate previousMonth = ld.minusMonths(1); // 减 1 个月
        LocalDate previousDay = ld.minusDays(1); // 减 1 天

        System.out.println("上一年: " + previousYear);
        System.out.println("上一个月: " + previousMonth);
        System.out.println("前一天: " + previousDay);

        // 5. 获取指定日期的 LocalDate 对象
        LocalDate specificDate = LocalDate.of(2022, 11, 12);
        System.out.println("指定日期: " + specificDate);

        // 6. 判断日期
        boolean isEqual = ld.isEqual(LocalDate.of(2022, 11, 12));
        boolean isBefore = ld.isBefore(LocalDate.of(2022, 11, 12));
        boolean isAfter = ld.isAfter(LocalDate.of(2022, 11, 12));

        System.out.println("是否相等: " + isEqual);
        System.out.println("是否在前: " + isBefore);
        System.out.println("是否在后: " + isAfter);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.

LocalDateTime可以转换为LocalDate和LocalTime,这两者也可以合并。

public class test {
    public static void main(String[] args) {
        // 创建一个 LocalDateTime 对象
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println("当前 LocalDateTime: " + ldt);

        // 将 LocalDateTime 转换为 LocalDate
        LocalDate ld = ldt.toLocalDate();
        System.out.println("转换后的 LocalDate: " + ld);

        // 将 LocalDateTime 转换为 LocalTime
        LocalTime lt = ldt.toLocalTime();
        System.out.println("转换后的 LocalTime: " + lt);

        // 使用 LocalDate 和 LocalTime 创建新的 LocalDateTime
        LocalDateTime newLdt = LocalDateTime.of(ld, lt);
        System.out.println("使用 LocalDate 和 LocalTime 创建的新的 LocalDateTime: " + newLdt);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

(2)时区时间ZoneId、ZoneDateTime

public class test {
    public static void main(String[] args) {
        // 1. ZoneId 的常见方法
        // 获取系统默认的时区
        ZoneId systemZoneId = ZoneId.systemDefault();
        System.out.println("系统默认的时区: " + systemZoneId);

        // 获取 Java 支持的全部时区 ID 列表
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        System.out.println("Java 支持的全部时区 ID: " + availableZoneIds);

        // 将指定的时区 ID 转换为 ZoneId 对象
        ZoneId zoneIdNewYork = ZoneId.of("America/New_York");
        System.out.println("纽约时区: " + zoneIdNewYork);

        // 2. ZonedDateTime 的常见方法
        // 获取指定时区的当前时间的 ZonedDateTime 对象
        ZonedDateTime dateTimeInNewYork = ZonedDateTime.now(zoneIdNewYork);
        System.out.println("纽约当前时间: " + dateTimeInNewYork);

        // 获取系统默认时区的当前时间的 ZonedDateTime 对象
        ZonedDateTime dateTimeSystem = ZonedDateTime.now();
        System.out.println("系统默认当前时间: " + dateTimeSystem);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

(3)Instant代替之前的Date

时间戳 

常用API 2_System_12

常用API 2_System_13

public class test {
    public static void main(String[] args) {
        // 获取当前时间的 Instant 对象
        Instant now = Instant.now();
        System.out.println("当前时间的 Instant 对象: " + now);

        // 获取从1970-01-01T00:00:00Z开始记录的秒数
        long epochSecond = now.getEpochSecond();
        System.out.println("从1970年开始的秒数: " + epochSecond);

        // 获取当前时间的纳秒数
        int nano = now.getNano();
        System.out.println("纳秒数: " + nano);

        // 增加时间
        Instant plusMillis = now.plusMillis(1000);
        Instant plusSeconds = now.plusSeconds(60);
        Instant plusNanos = now.plusNanos(1000);

        System.out.println("增加 1000 毫秒后的时间: " + plusMillis);
        System.out.println("增加 60 秒后的时间: " + plusSeconds);
        System.out.println("增加 1000 纳秒后的时间: " + plusNanos);

        // 减少时间
        Instant minusMillis = now.minusMillis(1000);
        Instant minusSeconds = now.minusSeconds(60);
        Instant minusNanos = now.minusNanos(1000);

        System.out.println("减少 1000 毫秒后的时间: " + minusMillis);
        System.out.println("减少 60 秒后的时间: " + minusSeconds);
        System.out.println("减少 1000 纳秒后的时间: " + minusNanos);

        // 比较和判断
        boolean isEqual = now.equals(plusMillis);
        boolean isBefore = now.isBefore(plusMillis);
        boolean isAfter = now.isAfter(minusMillis);

        System.out.println("是否相等: " + isEqual);
        System.out.println("是否在之前: " + isBefore);
        System.out.println("是否在之后: " + isAfter);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

(4)DateTimeFormatter代替之前的SimpleDateFormat

格式化器,用于时间的格式化、解析,线程安全

public static void main(String[] args) {
    // 1. 创建一个日期时间格式化器对象出来。
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

    // 2. 对时间进行格式化
    LocalDateTime now = LocalDateTime.now();
    System.out.println(now);

    String rs = formatter.format(now); // 正向格式化
    System.out.println(rs);

    String rs2 = now.format(formatter); // 反向格式化
    System.out.println(rs2);

    // 4. 解析时间:解析时间一般使用LocalDateTime提供的解析方法来解析。
    String dateStr = "2029年12月12日 12:12:11";
    LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter);
    System.out.println(dateTime);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

(5)Period、Duration

Period:计算两个LocalDate相差的年数、月数、天数。

public class test {
 public static void main(String[] args) {
  LocalDate startDate = LocalDate.of(2020, 1, 1);
  LocalDate endDate = LocalDate.of(2022, 12, 31);

  // 计算两个日期之间的差异
  Period period = Period.between(startDate, endDate);

  // 输出年、月、日差异
  System.out.println("Years: " + period.getYears());   // 输出: Years: 2
  System.out.println("Months: " + period.getMonths()); // 输出: Months: 11
  System.out.println("Days: " + period.getDays());     // 输出: Days: 30
 }

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

Duration:计算两个时间对象相差的天数、小时数、分数、秒数、纳秒数,支持的格式有LocalTime、LocalDateTime、Instant等。

public class test {
    public static void main(String[] args) {
        LocalDateTime startDateTime = LocalDateTime.of(2022, 1, 1, 0, 0);
        LocalDateTime endDateTime = LocalDateTime.of(2022, 1, 2, 0, 0);

        // 计算两个时间之间的差异
        Duration duration = Duration.between(startDateTime, endDateTime);

        // 输出天、小时、分钟、秒、毫秒、纳秒差异
        System.out.println("Days: " + duration.toDays());       // 输出: Days: 1
        System.out.println("Hours: " + duration.toHours());     // 输出: Hours: 24
        System.out.println("Minutes: " + duration.toMinutes()); // 输出: Minutes: 1440
        System.out.println("Seconds: " + duration.toSeconds()); // 输出: Seconds: 86400
        System.out.println("Millis: " + duration.toMillis());   // 输出: Millis: 86400000
        System.out.println("Nanos: " + duration.toNanos());     // 输出: Nanos: 86400000000000
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.