Java8 日期和時(shí)間類的基本使用
前言
最近在重構(gòu)之前的一個(gè)老項(xiàng)目,其中包含一個(gè)統(tǒng)計(jì)模塊,需要把存儲(chǔ)在MongoDB的數(shù)據(jù)通過接口顯示在后端管理系統(tǒng)中。這些數(shù)據(jù)大多是以時(shí)間為單位進(jìn)行存儲(chǔ),例如:collectionName_202009collectionName_20200910,在老系統(tǒng)中對(duì)時(shí)間的處理使用Date類,簡單了解了其中的時(shí)間工具類,深感繁瑣并決定使用Java8中的LocalDateTime和LocalDate重構(gòu)此代碼。
基本使用
1.獲取當(dāng)前時(shí)間
// 2020-08-23T20:14:56.977 LocalDateTime localDateTime = LocalDateTime.now(); //2020-08-23 LocalDate localDate = LocalDate.now();
2.格式化時(shí)間
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.獲取昨天、明天或者固定天數(shù)的時(shí)間
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(localDateTimeFormatter);
// 天數(shù)加5
LocalDateTime timePlus = localDateTime.plusDays(5L);
String timePlusStr = timePlus.format(localDateTimeFormatter);
// 天數(shù)減5
LocalDateTime timeMinus = localDateTime.minusDays(5L);
String timeMinusStr = timeMinus.format(localDateTimeFormatter);
在LocalDateTime的API中包含了對(duì)各個(gè)時(shí)間單位的增加和減少,如:

4.獲取今天的開始時(shí)間和結(jié)束時(shí)間,精確到秒
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()表示獲取今天的開始時(shí)間和結(jié)束時(shí)間,也可以換做任何一天
5.獲取當(dāng)月的第一天和最后一天
// 這里使用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(TemporalAdjusters.lastDayOfMonth());
6.將時(shí)間字符串轉(zhuǎn)為時(shí)間或日期
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.計(jì)算日期間隔
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);
以上就是Java8 日期和時(shí)間類的基本使用的詳細(xì)內(nèi)容,更多關(guān)于Java 日期和時(shí)間類的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
logback的使用和logback.xml詳解(小結(jié))
Logback是由log4j創(chuàng)始人設(shè)計(jì)的另一個(gè)開源日志組件,這篇文章主要介紹了logback的使用和logback.xml詳解(小結(jié)),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-11-11
Java將本地項(xiàng)目部署到Linux服務(wù)器的實(shí)踐
本文主要介紹了Java將本地項(xiàng)目部署到Linux服務(wù)器的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-06-06
基于Spring定時(shí)任務(wù)的fixedRate和fixedDelay的區(qū)別
這篇文章主要介紹了基于Spring定時(shí)任務(wù)的fixedRate和fixedDelay的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot3中數(shù)據(jù)庫集成實(shí)踐詳解
項(xiàng)目工程中,集成數(shù)據(jù)庫實(shí)現(xiàn)對(duì)數(shù)據(jù)的增曬改查管理,是最基礎(chǔ)的能力,所以下面小編就來和大家講講SpringBoot3如何實(shí)現(xiàn)數(shù)據(jù)庫集成,需要的可以參考下2023-08-08

