Java 日期和时间类的基本使用

网友投稿 543 2023-04-25

Java 日期和时间类的基本使用

Java 日期和时间类的基本使用

前言

最近在重构之前的一个老项目,其中包含一个统计模块,需要把存储在MongoDB的数据通过接口显示在后端管理系统中。这些数据大多是以时间为单位进行存储,例如:collectionName_202009collectionName_20200910,在老系统中对时间的处理使用Date类,简单了解了其中的时间工具类,深感繁琐并决定使用java8中的LocalDateTime和LocalDate重构此代码

基本使用

1.获取当前时间

// 2020-08-23T20:14:56.977

LocalDateTime localDateTime = LocalDateTime.now();

//2020-08-23

LocalDate localDate = LocalDate.now();

2.格式化时间

LocalDateTime localDateTime = LocalDateTime.now();

DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE);

// 2020-08-23 20:20:29

String timeStr = localDateTime.format(localDateTimeFormatter);

LocalDate localDate = LocalDate.now();

DateTimeFormatter localDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

// 2020-08-23

String dateStr = localDate.format(localDateFormatter);

3.获取昨天、明天或者固定天数的时间

LocalDateTime localDateTime = LocalDateTime.now();

DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE);

// 今天

String time = localDateTime.format(localDateTimeFormatter);

// 昨天

LocalDateTime yesterday = localDateTime.minusDays(1L);

String yesterdayStr = yesterday.format(localDateTimeFormatter);

// 后天

LocalDateTime tomorrow = localDateTime.plusDays(1L);

String tomorrowStr = tomorrow.format(localDatBOQlKcFaeTimeFormatter);

// 天数加5

LocalDateTime timePlus = localDateTime.plusDays(5L);

String timePlusStr = timePlus.format(localDateTimeFormatter);

// 天数减5

LocalDateTime timeMinus = localDateTime.minusDays(5L);

String timeMinusStr = timeMinus.format(localDateTimeFormatter);

在LocalDateTime的API中包含了对各个时间单位的增加和减少,如:

4.获取今天的开始时间和结束时间,精确到秒

DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE);

// 2020-08-23 00:00:00

String start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).format(localDateTimeFormatter);

// 2020-08-23 23:59:59

String end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).format(localDateTimeFormatter);

// 这里的LocalDate.now()表示获取今天的开始时间和结束时间,也可以换做任何一天

5.获取当月的第一天和最后一天

// 这里使用LocalDate来获取日期

DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE);

LocalDate localDate = LocalDate.now();

LocalDate firstDay = localDate.with(TemporalAdjusters.firstDayOfMonth());

LocalDate lastDay = localDate.with(Tempohttp://ralAdjusters.lastDayOfMonth());

6.将时间字符串转为时间或日期

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE);

String str = "2018-08-09 20:10:10";

LocalDateTime localDateTime = LocalDateTime.parse(str, formatter);

LocalDate localDate = LocalDate.parse(str, formatter);

7.计算日期间隔

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.SIMPLIFIED_CHINESE);

String str = "2020-09-02";

LocalDate localDate = LocalDate.parse(str, formatter);

long until = LocalDate.now().until(localDate, ChronoUnit.DAYS);

以上就是Java 日期和时间类的基本使用的详细内容,更多关于Java 日期和时间类的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:电脑版抖音打开小程序(电脑抖音app)
下一篇:Java Excel透视表相关操作实现代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~