Java LocalDateTime常用操作方法
Java8提供了新的时间接口LocalDateTime。本文主要介绍了Java8中LocalDateTime的一些常用操作方法。不多说,直接上代码。欲知详情,可以看看官网。
LocalDateTime localDateTime = LocalDateTime.now();
//时间运算,相加相减
System.out.println(localDateTime.plusYears(2)); //加2年
System.out.println(localDateTime.plusDays(2)); //加两天
System.out.println(localDateTime.minusYears(2)); //减两年
System.out.println(localDateTime.minusDays(2)); //减两天
System.out.println(localDateTime.toString()); // 转字符串 结果:2021-11-24T15:36:12.914
System.out.println(localDateTime.toLocalDate()); //获取日期(LocalDate) 结果:2021-11-24
System.out.println(localDateTime.toLocalTime()); //获取时间(LocalTime) 结果:15:36:12.914
System.out.println(localDateTime.getDayOfMonth()); //获取当前时间月份的第几天 结果:24
System.out.println(localDateTime.getDayOfWeek()); //获取当前周的第几天 结果:WEDNESDAY
System.out.println(localDateTime.getDayOfYear()); //获取当前时间在该年属于第几天 结果:328
System.out.println(localDateTime.getMonthValue()); // 获取当前时间的月份(阿拉伯文) 结果:11
System.out.println(localDateTime.getMonth()); // 获取当前时间的月份(英文) 结果:11
System.out.println(localDateTime.getHour()); // 获取当前时间的小时数 结果:15
System.out.println(localDateTime.getMinute()); // 获取当前时间的分钟数 结果:36
//格式化输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY/MM/dd HH:mm:ss");
System.out.println(localDateTime.format(formatter)); // 结果:2021/11/24 15:36:12
//构造时间
LocalDateTime startTime = LocalDateTime.of(2021, 1, 1, 20, 31, 20);
LocalDateTime endTime = LocalDateTime.of(2021, 1, 3, 20, 31, 20);
//比较时间
System.out.println(localDateTime.isAfter(startTime)); // 结果:true
System.out.println(localDateTime.isBefore(endTime)); // 结果:false
//获取毫秒数(使用Instant)
System.out.println(localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); // 结果:1637739570129
//获取秒数(使用Instant)
System.out.println(localDateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond()); // 结果:1637739570
// 获取当前时间的0点~23点
LocalDateTime beginDay = localDateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
LocalDateTime endDay = localDateTime.withHour(23).withMinute(59).withSecond(59);
// 获取本月的第一天的0点0分0秒和最后一天的23点59分59秒
LocalDateTime beginMonth = localDateTime.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
LocalDateTime endMonth = localDateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
// LocalDateTime转Date
Date date = Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
// Date转LocalDateTime
date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
// LocalDateTime获取秒数
Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
// LocalDateTime获取毫秒数
Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();效果如下:

v源码地址
https://github.com/toutouge/javademosecond/tree/master/hellolearn
到此这篇关于Java LocalDateTime实用方法的文章就介绍到这了,更多相关Java LocalDateTime实用方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringBoot整合Spring Data Elasticsearch的过程详解
这篇文章主要介绍了SpringBoot整合Spring Data Elasticsearch的过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-09-09
基于strict-origin-when-cross-origin问题的解决
这篇文章主要介绍了基于strict-origin-when-cross-origin问题的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-03-03
Spring Security实现基于RBAC的权限表达式动态访问控制的操作方法
这篇文章主要介绍了Spring Security实现基于RBAC的权限表达式动态访问控制,资源权限表达式动态权限控制在Spring Security也是可以实现的,首先开启方法级别的注解安全控制,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下2022-04-04
java使用lambda表达式对List集合进行操作技巧(JDK1.8)
这篇文章主要介绍了java使用lambda表达式对List集合进行操作技巧适用jdk1.8,感兴趣的朋友跟着小编一起看看实现代码吧2018-06-06


最新评论