• Java日期的学习篇


    关于日期的学习


    JDK8以前的API

    Date

    Date常用API

      
    在这里插入图片描述
      

    Date的API应用

    测试

    package com.xie.time;
    
    import java.util.Date;
    /**
     * JDK8以前的日期API
     * 目标:掌握Data日期类的使用
     * 重点:
     * 时间毫秒值 跟 日期对象 之间 的转化关系 要厘清
     * */
    public class DateTest {
        public static void main(String[] args) {
            // 创建一个Data的对象,代表的是系统的当前时间信息
            Date date1 = new Date();
            System.out.println(date1);
            // 获取时间毫秒值
            long times = date1.getTime();
            System.out.println(times);
            /** 把时间毫秒值 转换成 日期对象,计算2秒之后的时间
             * 利用有参构造器进行转化
             * */
            times += 2 * 1000;
            Date date2 = new Date(times);
            System.out.println(date2);
            /** 或通过setTime()方法直接进行修改 */
            Date date3 = new Date();
            date3.setTime(times);
            System.out.println(date3);
        }
    }
    
    • 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

    SimpleDateFormat

    SimpleDateFormat常用API

      
    在这里插入图片描述  


    格式化工作示意

      
    在这里插入图片描述
      

    测试
    package com.xie.time;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    /**
     * 优化Date,对日期格式进行格式化,使用户更加容易识别与接受
     *
     * 目标:掌握对于SimpleDateFormat的使用
     * 注:SimpleDateFormat代表简单日期格式化,可以用来 把日期对象、时间毫秒值 格式化成 我们想要的形式
     * */
    public class SimpleDateFormatTest {
        public static void main(String[] args) {
            // 获取时间对象及毫秒值
            Date date1 = new Date();
            long times = date1.getTime();
            /** 创建一个格式化对象,分别进行日期对象和时间毫秒值的格式化操作 */
            // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE a");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
            /** 进行时间对象格式化 */
            String format1 = sdf.format(date1);
            System.out.println(format1);
            /** 进行时间毫秒值格式化 */
            String format2 = sdf.format(times);
            System.out.println(format2);
        }
    }
    
    • 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

    反向格式化(逆操作)

      
    在这里插入图片描述
      

    测试
    package com.xie.time;
    
    import java.text.SimpleDateFormat;
    /**
     * 反向操作,把字符串时间格式 解析成 日期对象
     * 目标:掌握SimpleDateFormat解析字符串时间 成 日期对象
     * */
    public class SimpleDateFormatTest2 {
        public static void main(String[] args) throws Exception {
            // 设计一个字符串时间
            String dateStr = "2018-10-11 12:12:11";
            /** 创建简单日期格式化对象
             * 注:指定的时间格式 必须与 被解析的时间格式一模一样,否则程序会出bug。
             * */
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 解析并打印输出
            System.out.println(sdf.parse(dateStr));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    训练案例

    需求(秒杀活动)

      
    在这里插入图片描述
      

    实现
    package com.xie.time.case1;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    /**
     * 秒杀活动案例
     * 结果预测:小贾成功,小皮失败
     * */
    public class Shop {
        public static void main(String[] args) throws ParseException {
            /** 用变量记录秒杀活动案例开始时间、结束时间、小贾下单时间、小皮下单时间 */
            String startTime = "2023年11月11日 0:0:0";
            String endTime = "2023年11月11日 0:10:0";
            String xj = "2023年11月11日 0:01:18";
            String xp = "2023年11月11日 0:10:51";
            /** 创建简单格式化对象,把上面的字符串时间格式 解析成 日期对象 */
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            // 解析成日期对象
            Date startTimeFormat = sdf.parse(startTime);
            Date endTimeFormat = sdf.parse(endTime);
            Date xjFormat = sdf.parse(xj);
            Date xpFormat = sdf.parse(xp);
            /** 把日期对象 转化成 时间毫秒值 */
            long startTimes = startTimeFormat.getTime();
            long endTimes = endTimeFormat.getTime();
            long xjTimes = xjFormat.getTime();
            long xpTimes = xpFormat.getTime();
            /** 通过时间比对 判断 秒杀操作成功与否 */
    
            /**
             * 小贾
             * */
            if (xjTimes >= startTimes && xjTimes <= endTimes) {
                System.out.println("小贾秒杀成功了~~~");
            }else {
                System.out.println("小贾秒杀失败了~~~");
            }
    
            /**
             * 小皮
             * */
            if (xpTimes >= startTimes && xpTimes <= endTimes) {
                System.out.println("小皮秒杀成功了~~~");
            }else {
                System.out.println("小皮秒杀失败了~~~");
            }
        }
    }
    
    • 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

    Calendar

    需求痛点

      
    在这里插入图片描述
      
    在这里插入图片描述
      

    常见API

      
    在这里插入图片描述
      

    Calendar代表的是系统此刻时间对应的日历,通过它可以单独获取修改时间中的年、月、日、时、分、秒等。

    Calendar是可变对象,一旦修改后其对象本身表示的时间将产生变化

    应用测试
    package com.xie.time;
    
    import java.util.Calendar;
    import java.util.Date;
    /**
     * 日历对象的创建及使用
     * 目标:掌握Calender的使用和特点
     * */
    public class CalenderTest {
        public static void main(String[] args) {
            /** 得到系统此刻时间对应的日历对象 并打印输出*/
            Calendar calendar = Calendar.getInstance();
            System.out.println(calendar);
            // 获取日历中的某个信息,并打印输出
            System.out.println(calendar.get(Calendar.YEAR));
            System.out.println(calendar.get(Calendar.MONTH));
            System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
            System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
            // 获取日历中记录的日期对象
            Date date = calendar.getTime();
            System.out.println(date);
            // 获取到时间毫秒值
            long times = calendar.getTimeInMillis();
            System.out.println(times);
            System.out.println("---------------分隔符-----------------");
            /** 修改日历中的某位信息 */
            // 修改月份为11月,切记是从0开始遍历的
            calendar.set(Calendar.MONTH, 11);
            // 修改一年中的第几天 为第几天
            calendar.set(Calendar.DAY_OF_YEAR, 125);
            /** 增加或减少计算 */
            calendar.add(calendar.DAY_OF_YEAR, 100);
            calendar.add(calendar.DAY_OF_YEAR, -10);
            calendar.add(Calendar.DAY_OF_MONTH, 6);
            calendar.add(Calendar.HOUR, 12);
            // 打印输出查看修改效果
            System.out.println(calendar);
        }
    }
    
    • 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

    JDK8及以后的API(修改与新增)

    :很多方法都是返回不可变对象不同于JDK8之前的

    为啥学习(推荐使用)

      
    在这里插入图片描述
      


    新增的API

      
    在这里插入图片描述

      
    在这里插入图片描述

      


    Local…(或Local开头)API创建对象的方案

      
    在这里插入图片描述
      

    LocalDate常用API

      
    在这里插入图片描述
      

    应用测试

    package com.xie.time.local;
    
    import java.time.DayOfWeek;
    import java.time.LocalDate;
    import java.time.Month;
    /**
     * LocalDate API应用测试
     * */
    public class LocalDateTest {
        public static void main(String[] args) {
            /** 获取本地日期对象(不可变对象,而JDK8之前是可变对象) */
            LocalDate dateObject = LocalDate.now();
            System.out.println(dateObject);
            // 1、获取日期对象中的信息
            int year = dateObject.getYear();
            Month month = dateObject.getMonth();
            int dayOfMonth = dateObject.getDayOfMonth();
            int dayOfYear = dateObject.getDayOfYear();
            DayOfWeek dayOfWeek = dateObject.getDayOfWeek();
            System.out.println(dayOfWeek);
            int value = dateObject.getDayOfWeek().getValue();
            System.out.println(value);
            System.out.println(year);
            // 2、直接修改某个信息: withYear、withMonth、withDayOfMonth、withDayOfYear
            /**
             * 每次修改都会返回一个 新的 日期对象
             * */
            System.out.println("------------------------------------");
            LocalDate dateObject2 = dateObject.withYear(2099);
            LocalDate dateObject3 = dateObject.withMonth(12);
            System.out.println("新的年-->" + dateObject2);
            System.out.println("旧的年-->" + dateObject);
            System.out.println("新的月-->" + dateObject3);
            // 3、把某个信息加多少: plusYears、plusMonths、plusDays、plusWeeks
            /**
             * 同样,每次修改都会返回一个 新的 日期对象
             * */
            System.out.println("================================");
            LocalDate dateObject4 = dateObject.plusYears(2);
            System.out.println("加了2年-->" + dateObject4);
            // 4、把某个信息减多少: minusYears、minusMonths、minusDays、minusWeeks
            /**
             * 同样,每次修改都会返回一个 新的 日期对象
             * */
            System.out.println("================================");
            LocalDate dateObject5 = dateObject.minusYears(2);
            System.out.println("减了2年-->" + dateObject5);
            LocalDate dateObject6 = dateObject.minusMonths(2);
            System.out.println("减了2月-->" + dateObject6);
            // 5、获取指定日期的LocalDate对象: public static LocalDate of(int year,int month, int dayOfMonth)
            /**
             * 同样,每次修改都会返回一个 新的 日期对象
             * */
            System.out.println("================================");
            LocalDate dateObject7 = dateObject.of(2099, 12, 1);
            LocalDate dateObject8 = dateObject.of(2099, 12, 1);
            System.out.println("指定日期-->" + dateObject7);
            // 6、判断2个日期对象,是否相等,在前还是在后: equals isBefore isAfter
            /**
             * 同样,每次修改都会返回一个 新的 日期对象
             * */
            System.out.println("================================");
            System.out.println(dateObject7.equals(dateObject8));// true
            System.out.println(dateObject7.isAfter(dateObject));// true
            System.out.println(dateObject8.isBefore(dateObject));// false
        }
    }
    
    • 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
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    LocalTime常用API

      
    在这里插入图片描述
      

    应用测试

    package com.xie.time.local;
    
    import java.time.LocalTime;
    /**
     * LocalTime API应用测试
     * */
    public class LocalTimeTest {
        public static void main(String[] args) {
            /** 获取本地时间对象 */
            LocalTime time = LocalTime.now();
            System.out.println(time);
            // 获取时间对象中的信息
            int hour = time.getHour();//时
            int minute = time.getMinute();//分
            int second = time.getSecond();//秒
            int nano = time.getNano();//纳秒
            // 2、修改时间: withHour、withMinute、withSecond、withNano
            LocalTime time3 = time.withHour(10);
            LocalTime time4 = time.withMinute(10);
            LocalTime time5 = time.withSecond(10);
            LocalTime time6 = time.withNano(10);
            // 3、加多少: pLusHours、plusMinutes、plusSeconds、plusNanos
            LocalTime time7 = time.plusHours(10);
            LocalTime time8 = time.plusMinutes(10);
            LocalTime time9 = time.plusSeconds(10);
            LocalTime time10= time.plusNanos(10);
            // 4、减多少:minusHours、minusMinutes、minusSeconds、minusNanos
            LocalTime time11 = time.minusHours(10);
            LocalTime time12 = time.minusMinutes(10);
            LocalTime time13 = time.minusSeconds(10);
            LocalTime time14 = time.minusNanos(10);
            // 5、获取指定时间的LocalTime对象:
            // public static LocalTime of(int hour,int minute,int second)
            LocalTime time15 = LocalTime.of( 12,12 ,12);
            LocalTime time16 = LocalTime.of(12,12 ,12);
            // 6、判断2个时间对象,是否相等,在前还是在后:equals isBefore isAfter
            System.out.println(time15.equals(time16));
            System.out.println(time15.isAfter(time));
            System.out.println(time5.isBefore(time));
        }
    }
    
    • 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
    LocalDateTime常用API

      
    在这里插入图片描述
      

    应用测试

    package com.xie.time.local;
    
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.LocalTime;
    /**
     * LocalDateTime API应用测试
     * */
    public class LocalDateTimeTest {
        public static void main(String[] args) {
            /** 获取本地日期时间对象 */
            LocalDateTime dateTime = LocalDateTime.now();
            System.out.println(dateTime);
            // 1、可以获取日期和时间的全部信息
            int year = dateTime.getYear(); // 年
            int month = dateTime.getMonthValue(); // 月
            int day = dateTime.getDayOfMonth(); // 日
            int dayOfYear = dateTime.getDayOfYear(); // 一年中的第几天
            int dayOfWeek = dateTime.getDayOfWeek().getValue(); // 获取是周几
            int hour = dateTime.getHour(); //时
            int minute = dateTime.getMinute(); //分
            int second = dateTime.getSecond(); //秒
            int nano = dateTime.getNano(); //纳秒
    
            // 3、加多少:
            // plusYears plusMonths plusDays plusWeeks plusHours plusMinutes plusSeconds plusNanos
            LocalDateTime dateTime4 = dateTime.plusYears(2);
            LocalDateTime dateTime5 = dateTime.plusMinutes(3);
    
            // 4、减多少:
            // minusDays minusYears minusMonths minusWeeks minusHours minusMinutes minusSeconds minusNanos
            LocalDateTime dateTime6 = dateTime.minusYears(2);
            LocalDateTime dateTime7 = dateTime.minusMinutes(3);
    
            // 5、获取指定日期和时间的LocalDateTime对象:
            // public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour,
            //                                int minute, int second, int nanoOfSecond)
            LocalDateTime dateTime8 = dateTime.of(2029, 12, 12, 12, 12, 12, 1222);
            LocalDateTime dateTime9 = dateTime.of(2029, 12, 12, 12, 12, 12, 1222);
            // 6、 判断2个日期、时间对象,是否相等,在前还是在后:equals、isBefore、isAfter
            System.out.println(dateTime9.equals(dateTime));
            System.out.println(dateTime9.isAfter(dateTime));
            System.out.println(dateTime9.isBefore(dateTime));
    
            // 7、可以把LocalDateTime 转换成 LocalDate 和 LocalTime
            // public LocalDate toLocalDate()
            // public LocalTime toLocalTime()
            // public static LocalDateTime of(LocalDate date, LocalTime time)
            System.out.println("================================");
            LocalDate date = dateTime.toLocalDate();
            LocalTime time = dateTime.toLocalTime();
            LocalDateTime dateTime2 = LocalDateTime.of(date, time);
            System.out.println(dateTime2 + "-->" + date + " && " +time);
        }
    }
    
    • 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
    Zone开头的

      
    在这里插入图片描述
      

    应用测试

    package com.xie.time.zone;
    
    import java.time.Clock;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;
    /**
     * 目标:了解时区 和 带时区的时间
     * */
    public class ZoneIdAndZonedDateTimeTest {
        public static void main(String[] args) {
            /** 获取系统默认时区 */
            ZoneId zoneId = ZoneId.systemDefault();
            System.out.println(zoneId.getId());
            System.out.println(zoneId);
            /** 获取Java支持的全部时区Id */
            System.out.println(ZoneId.getAvailableZoneIds().size());
            System.out.println(ZoneId.getAvailableZoneIds());
            /** 把某个时区id 封装成 ZoneId对象 */
            ZoneId zoneId1 = ZoneId.of("America/New_York");
            /** 获取某个时区的时间 即先获取ZonedDateTime对象 */
            ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId1);
            System.out.println(zonedDateTime);
            /** 获取世界标准时间 */
            System.out.println(ZonedDateTime.now(Clock.systemUTC()));
            /** 获取 系统 默认时区 的ZonedDateTime对象 */
            ZonedDateTime now = ZonedDateTime.now();
            System.out.println(now);
        }
    }
    
    • 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
    Instant(某个时刻/时间戳)API

    :可以用来记录代码的执行时间,或用于记录用户操作某个事件的时间点

      在这里插入图片描述  

    应用测试

    package com.xie.time;
    
    import java.time.Instant;
    /**
     * 目标:掌握对Instant的使用
     * Instant:时间线上的某个时刻/时间戳
     * */
    public class InstantTest {
        public static void main(String[] args) {
            /** 创建Instant对象,获取此刻的时间信息 */
            Instant instant = Instant.now();
            /** 获取总秒数 */
            long epochSecond = instant.getEpochSecond();
            System.out.println(epochSecond);
            /** 获取不够一秒的纳秒数 */
            System.out.println(instant.getNano());
    
            System.out.println(instant);
            /** 修改方法使用,示例,许多方法同LocalDateTime对象的 */
            Instant instant1 = instant.plusNanos(1111);
            System.out.println(instant1);
            System.out.println("================================================");
            // Instant对象的作用:做代码的性能分析,或者记录用户的操作时间点
            Instant instant2 = Instant.now();
            // 代码执行。。。
            Instant instant3 =Instant.now();
        }
    }
    
    • 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
    DateTimeForMatter(时间格式化器)API

      
    在这里插入图片描述

      
    在这里插入图片描述
      
    在这里插入图片描述
      

    应用测试

    package com.xie.time;
    
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    /**
     * 格式化器
     * */
    public class DateTimeForMatterTest {
        public static void main(String[] args) {
            /** 创建 一个 日期时间 格式化器 对象 */
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
            /** 获取时间 */
            LocalDateTime localDateTime1 = LocalDateTime.now();
            System.out.println(localDateTime1);
            /** 对时间 进行格式化 正向格式化 */
            String format1 = dateTimeFormatter.format(localDateTime1);
            System.out.println(format1);
            /** 格式化另一种方案,用LocalDateTime对象的方法进行 我们称之为反向格式化,是相较于上面一种的 */
            String format2 = localDateTime1.format(dateTimeFormatter);
            System.out.println(format2);
            /** 解析时间: 解析时间一般使用LocalDateTime提供的解析方法来解析 */
            // 设置时间
            String dateStr = "2029年12月12日 12:12:11";
            LocalDateTime localDateTime2 = LocalDateTime.parse(dateStr, dateTimeFormatter);
            System.out.println(localDateTime2);
        }
    }
    
    • 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
    Period常用API

      
    在这里插入图片描述
      

    应用测试

    package com.xie.time;
    
    import java.time.LocalDate;
    import java.time.Period;
    /**
     * Period常用API的使用
    * */
    public class PeriodTest {
        public static void main(String[] args) {
            /**通过 指定时间 获取 两个日期对象*/
            LocalDate startTime = LocalDate.of(2029, 8, 10);
            LocalDate endTime = LocalDate.of(2035, 12, 20);
            /** 创建Period对象,封装两个时间点的 日期对象 */
            Period period = Period.between(startTime, endTime);
            /** 通过period对象 来获取 两个日期对象 相差的信息 */
            System.out.println("两者相差 " + period.getYears() + " 年");
            System.out.println("两者相差 " + period.getMonths() + " 月");
            System.out.println("两者相差 " + period.getDays() + " 日或天");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    Duration常用API

      
    在这里插入图片描述
      

    应用测试

    package com.xie.time;
    
    import java.time.Duration;
    import java.time.LocalDateTime;
    /**
     * Duration常用API的使用
     * */
    public class DurationTest {
        public static void main(String[] args) {
            /**通过 指定时间 获取 两个日期时间对象*/
            LocalDateTime startDateTime = LocalDateTime.of(2035, 8, 10, 0, 0, 0);
            LocalDateTime endDateTime = LocalDateTime.of(2035, 8, 15, 6, 5, 5);
            // 1、获取Duration对象
            Duration duration = Duration.between(startDateTime, endDateTime);
            // 2、获取两个日期时间对象的间隔信息
            System.out.println("两者相差 " + duration.toDays() + " 天");// 间隔多少天
            System.out.println("两者相差 " + duration.toHours() + " 小时");// 间隔多少小时
            System.out.println("两者相差 " + duration.toMinutes() + " 分");// 间隔多少分
            System.out.println("两者相差 " + duration.getSeconds() + " 秒");// 间隔名少秒
            System.out.println("两者相差 " + duration.toMillis() + " 毫秒");// 间隔多少毫秒
            System.out.println("两者相差 " + duration.toNanos() + " 纳秒");// 间隔多少纳秒
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    拓展

    时间格式符号

      
    在这里插入图片描述
      

    参考视频

    黑马磊哥


  • 相关阅读:
    Matplotlib--绘图标记
    纯文本邮件发送:java
    保存save_data()数组有[]的解决办法
    Taurus.mvc .Net Core 微服务开源框架发布V3.1.7:让分布式应用更高效。
    网络安全之了解安全托管服务(MSS)
    C#中DataAdapter对象
    SAP smartform和ALV如何使用图片 & 如何下载SE78上传的图片到本地
    行为型模式-备忘录模式
    docker搭建redis哨兵集群并且整合springboot
    通用返回结果类ResultVO
  • 原文地址:https://blog.csdn.net/m0_69604107/article/details/133590743