JDK1.8中的时间处理方法

        //jDK1.8格式化时间的方法
        //获取当前时间
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println("当前时间:" + dateTimeFormatter.format(localDateTime));
        //修改时间
        LocalDateTime changeDate = LocalDateTime.of(2020, 3, 13, 23, 00, 00);
        System.out.println("修改后时间:" + dateTimeFormatter.format(changeDate));
        //比较时间相差
        Duration between = Duration.between(localDateTime, changeDate);
        System.out.println("相差多少天:" + between.toDays());//相差几天
        System.out.println("相差多少小时:" + between.toHours());//相差几天
        System.out.println("相差多少分钟:" + between.toMinutes());//相差几天
        System.out.println("相差多少毫秒:" + between.toMillis());//相差几天
        System.out.println("相差多少纳秒:" + between.toNanos());//相差几天

运行结果如下

        //获取当前系统的时间
        LocalDate today = LocalDate.now();//不包含具体时间的日期
        // LocalTime localTime = LocalTime.now(); 不包含日期的时间
        //LocalDateTime localDateTime = LocalDateTime.now(); 包含日期及时间LocalDateTime
        System.out.println("今天日期:" + today);
        System.out.println("现在是那年:" + today.getYear());
        System.out.println("现在是那月:" + today.getMonth());
        System.out.println("现在是那月数字形式:" + today.getMonthValue());
        System.out.println("现在是几号" + today.getDayOfMonth());
        System.out.println("现在是周几:" + today.getDayOfWeek());
        //加减年份
        LocalDate changeDate1 = today.plusYears(1);
        System.out.println("加后是那年" + changeDate1.getYear());
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

 

运行结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值