java8新特性--时间API的使用(LocalDate,LocalTime,LocalDateTime,Instant,Duration,Period)

本文介绍了Java中LocalDate、LocalTime、Instant和Period类的使用,包括获取当前时间、设置特定时间、时间间隔计算以及日期间隔分析。通过实例展示了如何操作这些时间类型并获取其中的详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.LocalDate LocalTime LocalDateTime

//1.LocalDate   LocalTime   LocalDateTime
    @Test
    public void test1(){
        //获取当前时间
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println("ldt: "+ldt);

        //设置时间
        LocalDateTime ldt2 = LocalDateTime.of(2021,10,11,13,22,21);
        System.out.println("ldt2: "+ldt2);

        //增加年份
        LocalDateTime ldt3 = ldt.plusYears(2);
        System.out.println("ldt3: "+ldt3);

        //减少月份
        LocalDateTime ldt4 = ldt.minusMonths(2);
        System.out.println("ldt4: "+ldt4);

        //分别获取年月日时分秒
        System.out.println("ldt的年: "+ldt.getYear());
        System.out.println("ldt的月: "+ldt.getMonthValue());
        System.out.println("ldt的日: "+ldt.getDayOfMonth());
        System.out.println("ldt的时: "+ldt.getHour());
        System.out.println("ldt的分: "+ldt.getMinute());
        System.out.println("ldt的秒: "+ldt.getSecond());
    }

运行结果

ldt: 2021-10-24T21:04:44.169207900
ldt2: 2021-10-11T13:22:21
ldt3: 2023-10-24T21:04:44.169207900
ldt4: 2021-08-24T21:04:44.169207900
ldt的年: 2021
ldt的月: 10
ldt的日: 24
ldt的时: 21
ldt的分: 4
ldt的秒: 44

2.Instant :时间戳(以Unix元年: 1970年1月1日 00:00:00 到 某个时间之间的毫秒值)

//2.Instant :时间戳(以Unix元年: 1970年1月1日 00:00:00 到 某个时间之间的毫秒值)
    @Test
    public void test2(){
        //获取UTC时区的时间,与china当地时差8个小时
        Instant ins1 = Instant.now();//默认获取UTC时区
        System.out.println(ins1);

        //增加UTC时区时间,增加10小时  并会显示+10:00
        OffsetDateTime odt = ins1.atOffset(ZoneOffset.ofHours(10));
        System.out.println(odt);

        //获取UTC时区的时间,并将值转化为时间戳
        System.out.println(ins1.toEpochMilli());

        //设置时间:1970年1月1日 00:00:00的基础上增加60秒
        Instant ins2 = Instant.ofEpochSecond(60);
        System.out.println(ins2);
    }

运行结果

2021-10-24T13:08:20.496456300Z
2021-10-24T23:08:20.496456300+10:00
1635080900496
1970-01-01T00:01:00Z

3.Duration :计算两个时间之间的间隔

//3.Duration :计算两个时间之间的间隔
    @Test
    public void test3(){
        Instant instant1 = Instant.now();
        try {
            Thread.sleep(1000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        Instant instant2 = Instant.now();
        Duration duration = Duration.between(instant1, instant2);
        System.out.println(duration.toMillis());

        System.out.println("----------------------------------");

        LocalTime lt1 = LocalTime.now();
        try {
            Thread.sleep(1000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        LocalTime lt2 = LocalTime.now();
        System.out.println(Duration.between(lt1, lt2).toMillis());
    }

运行结果

1001
----------------------------------
1003
  1. Period :计算两个“日期”之间的间隔
//4. Period :计算两个“日期”之间的间隔
    @Test
    public void test4(){
        LocalDate ld1 = LocalDate.of(2016, 1, 1);
        LocalDate ld2 = LocalDate.now();

        Period period = Period.between(ld1, ld2);
        System.out.println(period);

        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }

运行结果

P5Y9M23D
5
9
23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值