JAVA 8 In Action 读书笔记 (四) : LocalDateTime , Instant

测试源码

public class Demo {

    public static void main(String[] args) {
        System.out.println("=================日期处理================");
        //不同方式生成日期
        LocalDate date1= LocalDate.now();
        System.out.println("date1: "+date1);
        //date1: 2017-11-23

        LocalDate date2 = LocalDate.parse("2013-10-23");
        System.out.println("date2: "+date2);
        //date2: 2013-10-23

        LocalDate date3= LocalDate.of(2013, 11, 22);
        System.out.println("date3: "+date3);
        //date3: 2013-11-22

        //日期细节的获取
        System.out.println(date3.getYear()+"-"+date3.get(ChronoField.MONTH_OF_YEAR)+"-"+date3.get(ChronoField.DAY_OF_MONTH));
        // 2013-11-22

        //月末
        LocalDate date4= date3.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("date4: "+date4);
        //date4: 2013-11-30

        System.out.println("=================时间处理================");
        //不同方式生成时间
        LocalTime time1= LocalTime.now();
        System.out.println("time1: "+time1);
        //time1: 13:22:04.363056100

        LocalTime time2 = LocalTime.parse("21:10:22");
        System.out.println("time2: "+time2);
        //time2: 21:10:22

        LocalTime time3 = LocalTime.of(0, 11, 22);
        System.out.println("time3: "+time3);
        // time3: 00:11:22

        //时间细节的获取
        System.out.println("time3(2): "+time3.getHour()+":"+time3.get(ChronoField.MINUTE_OF_HOUR)+":"+time3.get(ChronoField.SECOND_OF_MINUTE));
        // time3(2): 0:11:22

        System.out.println("=================日期时间处理================");
        //日期和时间
        LocalDateTime dateTime1= LocalDateTime.parse("2015-10-10T15:15:00");
        System.out.println("dateTime1: "+dateTime1);
        //dateTime1: 2015-10-10T15:15

        //DateTimeFormatter是线程安全的       
        LocalDateTime dateTime2= LocalDateTime.parse("2015-10-10 15:15:01",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("dateTime2: "+dateTime2.format(DateTimeFormatter.ISO_DATE_TIME));
        //dateTime2: 2015-10-10T15:15:01
        //
        ZonedDateTime zonedDateTime= dateTime1.atZone(TimeZone.getDefault().toZoneId());
        System.out.println("zonedDateTime: "+zonedDateTime);
        System.out.println("=================Instant处理================");
        //zonedDateTime: 2015-10-10T15:15+08:00[Asia/Shanghai]

        // Instant
        Instant instant1 = Instant.now();
        System.out.println("instant1: "+instant1);
        //instant1: 2017-11-23T05:22:04.372058Z

        //仅仅是一个测试用例,不用这样使用
        Instant instant2 =Instant.ofEpochMilli((new Date()).getTime());
        System.out.println("instant2: "+instant2);
        //instant2: 2017-11-23T05:22:04.373Z

        //解析时间
        Instant instant3 = Instant.parse("2012-10-10T10:20:21Z");
        System.out.println("instant3: "+instant3);
        //instant3: 2012-10-10T10:20:21Z

        // 日期操作
        Instant instant4 = instant3.plus(10, ChronoUnit.DAYS);
        System.out.println("instant4: "+instant4);
        //instant4: 2012-10-20T10:20:21Z

        Instant instant5 = dateTime1.toInstant(ZoneOffset.UTC);
        System.out.println("instant5: "+instant5);
        //instant5: 2015-10-10T15:15:00Z

        //
        Duration d1= Duration.between(instant3, instant4);
        System.out.println("Duration1: "+d1);
        //Duration1: PT240H

        Duration d2= Duration.between(dateTime2, dateTime1);
        System.out.println("Duration2: "+d2);
        // Duration2: PT-1S

        Duration d3= Duration.between(instant3, zonedDateTime);
        System.out.println("Duration3: "+d1);
        //Duration3: PT240H 

        //
    }

}

完整输出:

=================日期处理================
date1: 2017-11-23
date2: 2013-10-23
date3: 2013-11-22
2013-11-22
date4: 2013-11-30
=================时间处理================
time1: 13:22:04.363056100
time2: 21:10:22
time3: 00:11:22
time3(2): 0:11:22
=================日期时间处理================
dateTime1: 2015-10-10T15:15
dateTime2: 2015-10-10T15:15:01
zonedDateTime: 2015-10-10T15:15+08:00[Asia/Shanghai]
=================Instant处理================
instant1: 2017-11-23T05:22:04.372058Z
instant2: 2017-11-23T05:22:04.373Z
instant3: 2012-10-10T10:20:21Z
instant4: 2012-10-20T10:20:21Z
instant5: 2015-10-10T15:15:00Z
Duration1: PT240H
Duration2: PT-1S
Duration3: PT240H

Github源码
Eclipse工程源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值