JDK8日期相关

		LocalDateTime now = LocalDateTime.now();
        //获取当前日期
        System.out.println("now = " + now);//now = 2020-01-27T22:23:33.320
        System.out.println("now.getYear() = " + now.getYear());//now.getYear() = 2019
        //注意获取月份的两个方法的区别
        System.out.println("now.getMonth() = " + now.getMonth());//now.getMonth() = JANUARY
        System.out.println("now.getMonthValue() = " + now.getMonthValue());//now.getMonthValue() = 1
        System.out.println("now.getDayOfMonth() = " + now.getDayOfMonth());//now.getDayOfMonth() = 27
        System.out.println("now.getHour() = " + now.getHour());
        System.out.println("now.getMinute() = " + now.getMinute());
        System.out.println("now.getSecond() = " + now.getSecond());

        //构造指定日期,eg.2020-08-08
        LocalDateTime diyDate = LocalDateTime.of(2020, 8, 8, 0, 0, 0);
        System.out.println("diyDate = " + diyDate);//diyDate = 2020-08-08T00:00
        
        //修改日期
        //①增加/减少几个月
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime = " + localDateTime);//localDateTime = 2020-01-27T22:37:05.546
        //下个月,plusMonth()
        localDateTime = localDateTime.plusMonths(1);
        System.out.println("localDateTime = " + localDateTime);//localDateTime = 2020-02-27T22:37:05.546
        //上个月,-1
        localDateTime = localDateTime.minusMonths(1);
        //或者使用 plusMonth(-1)
        //localDateTime = localDateTime.plusMonths(-1);
        System.out.println("localDateTime = " + localDateTime);//localDateTime = 2020-01-27T22:37:05.546

        //②修改到指定日期
        localDateTime = localDateTime.withYear(2200);
        System.out.println("localDateTime = " + localDateTime);//localDateTime = 2200-01-27T22:43:26.722

        //格式化日期
        //①自定义日期格式
        String diyFormatDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("diyFormatDate = " + diyFormatDate);//formatDate = 2020-01-27 22:47:21
        //②几种基本格式
        String format = now.format(DateTimeFormatter.ISO_DATE);
        System.out.println("format = " + format);//format = 2020-01-27
        format = now.format(DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println("format = " + format);//format = 20200127

        //解析日期
        LocalDateTime parse = LocalDateTime.parse("2020-01-27 22:47:21", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("parse = " + parse);//parse = 2020-01-27T22:47:21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值