Java Date、LocalDate、LocalDateTime、时间戳常见用法及转换

代码实例

		LocalDate localDate = LocalDate.now();
        //时间戳转LocalDateTime
        long currentTimeMillis = System.currentTimeMillis();
        LocalDateTime currentTimeMillisLocalDateTime = LocalDateTime.ofEpochSecond(currentTimeMillis / 1000, 0, ZoneOffset.ofHours(8));

        //Date转LocateDateTime
        LocalDateTime dateCastToLocalDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());

        //Date转LocalDate
        Date date = new Date();
        LocalDate dateCastToLocalDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();

        //LocalDateTime转Date
        Date localDateTimeCastDate = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());

        //LocalDate转Date
        ZonedDateTime zonedDateTime = LocalDate.now().atStartOfDay(ZoneId.systemDefault());
        Date localDateCastDate = Date.from(zonedDateTime.toInstant());

        //日期格式化
        LocalDate parseDate = LocalDate.parse("2022-06-07");

        //增加一天后的日期,
        LocalDate plusDate = localDate.plus(1, ChronoUnit.DAYS);

        //减少一天后的日期
        LocalDate minusDay = localDate.minus(1, ChronoUnit.DAYS);

        //获取年
        int year = localDate.getYear();

        //获取月
        int monthValue = localDate.getMonthValue();

        //时间比较
        boolean after = plusDate.isAfter(minusDay);
        boolean before = parseDate.isBefore(minusDay);
        boolean equal = parseDate.isEqual(minusDay);

        System.out.println("currentTimeMillisLocalDateTime=" + currentTimeMillisLocalDateTime.toString());
        System.out.println("dateCastToLocalDateTime=" + dateCastToLocalDateTime.toString());
        System.out.println("dateCastToLocalDate=" + dateCastToLocalDate.toString());
        System.out.println("localDateTimeCastDate=" + localDateTimeCastDate.toString());
        System.out.println("localDateCastDate=" + localDateCastDate.toString());
        System.out.println("parseDate=" + parseDate.toString());
        System.out.println("plusDate=" + plusDate.toString());
        System.out.println("minusDay=" + minusDay.toString());
        System.out.println("year=" + year);
        System.out.println("monthValue=" + monthValue);
        System.out.println("after=" + after);
        System.out.println("before=" + before);
        System.out.println("equal=" + equal);

运行结果

currentTimeMillisLocalDateTime=2022-06-07T21:20:53
dateCastToLocalDateTime=2022-06-07T21:20:53.989
dateCastToLocalDate=2022-06-07
localDateTimeCastDate=Tue Jun 07 21:20:53 CST 2022
localDateCastDate=Tue Jun 07 00:00:00 CST 2022
parseDate=2022-06-07
plusDate=2022-06-08
minusDay=2022-06-06
year=2022
monthValue=6
after=true
before=false
equal=false
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值