Java中LocalDateTime的具體用法
一.背景
本文主要介紹Java 8中時間的操作方法
- java.util.Date是用于表示一個日期和時間的對象(注意與java.sql.Date區(qū)分,后者用在數(shù)據(jù)庫中沒有格式化的Date),它打印出的日期可讀性差,可以使用SimpleDateFormat對時間進行格式化,但SimpleDateFormat又是線程不安全,包括format和parse方法,而在時間的計算方面不是很方便。
- java.util.Canlendar 可以用于獲取并設(shè)置年、月、日、時、分、秒,它和Date比,主要多了一個可以做簡單的日期和時間運算的功能,Canlendar 變量是全局變量,會導(dǎo)致臟變量情況產(chǎn)生,并且這個共享變量沒有做線程安全控制,也就是多線程的情況下是線程不安全的。
- Java8出的新的時間日期API都是線程安全的比如LocalDate、LocalTime、LocalDateTime這三個類,計算功能強大,并且性能更好,代碼更簡潔。
- 進行相關(guān)實例時,請先確保已安裝JDK8,本文采用的版本是:jdk1.8.0_111
二.簡介
- LocalDate :表示當(dāng)前日期,相當(dāng)于:yyyy-MM-dd
- LocalTime :表示當(dāng)前時間,相當(dāng)于:HH:mm:ss (24小時制) 或者 hh:mm:ss(12小時制)
- LocalDateTime :表示當(dāng)前日期時間,相當(dāng)于:yyyy-MM-ddTHH:mm:ss ,是前兩者的結(jié)合
- DateTimeFormatter :表示格式化類型,可以取代SimpleDateFormat
- Instant :表示時刻,用來表示時間線上的一個點(瞬時),也可以說是時間戳
- ZoneId、ZoneOffset :表示時區(qū)
- ZonedDateTime :表示帶時區(qū)的日期和時間,是前兩者的結(jié)合
- Duration :表示兩個時刻之間的時間間隔
- Period :表示兩個日期之間的天數(shù)
三.實戰(zhàn)
3.1 LocalDate的創(chuàng)建與使用
3.1.1、LocalDate的創(chuàng)建
/** * LocalDate的初始化方法 * 此處單元測試的注解是采用:org.junit.Test */ @Test public void init(){ //1.創(chuàng)建LocalDate,LocalDate對象直接調(diào)用now(),獲取到當(dāng)前日期 LocalDate localDateNow = LocalDate.now(); System.out.println("1.直接調(diào)用now()創(chuàng)建:" + localDateNow); //2.直接根據(jù)(年月日)創(chuàng)建,如:2021-05-20 LocalDate localDateOf = LocalDate.of(2021, 5, 20); System.out.println("2.根據(jù)年月日創(chuàng)建:" + localDateOf); //3.直接根據(jù)某一年的某一天創(chuàng)建,如 2021年中第200天 LocalDate localDateOfYearDay = LocalDate.ofYearDay(2021, 200); System.out.println("3.根據(jù)某一年的某一天創(chuàng)建:" + localDateOfYearDay); //4.根據(jù)java.time.temporal.TemporalAccessor創(chuàng)建(包括LocalDate,LocalDateTime等等) LocalDate localDateFrom = LocalDate.from(LocalDate.now()); System.out.println("4.根據(jù)TemporalAccessor創(chuàng)建:" + localDateFrom); //5.根據(jù)時間字符串轉(zhuǎn)化創(chuàng)建,調(diào)用parse(CharSequence text)方法,此方法只支持yyyy-MM-dd格式的值 LocalDate localDateParse = LocalDate.parse("2021-05-11"); System.out.println("5.根據(jù)時間字符串轉(zhuǎn)化創(chuàng)建:" + localDateParse); //6.根據(jù)時間字符串轉(zhuǎn)化創(chuàng)建,調(diào)用parse(CharSequence text, DateTimeFormatter formatter)方法,此時的text可以根據(jù)formatter多變 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); LocalDate localDateParse2 = LocalDate.parse("20210515", formatter); System.out.println("6.根據(jù)時間字符串轉(zhuǎn)化創(chuàng)建:" + localDateParse2); }
運行結(jié)果:
1.直接調(diào)用now()創(chuàng)建:2021-05-24
2.根據(jù)年月日創(chuàng)建:2021-05-20
3.根據(jù)某一年的某一天創(chuàng)建:2021-07-19
4.根據(jù)TemporalAccessor創(chuàng)建:2021-05-24
5.根據(jù)時間字符串轉(zhuǎn)化創(chuàng)建:2021-05-11
6.根據(jù)時間字符串轉(zhuǎn)化創(chuàng)建:2021-05-15
3.1.2、LocalDate的常見使用方法
/** * LocalDate的常用使用方法 * 此處單元測試的注解是采用:org.junit.Test */ @Test public void usage() { //此處采用LocalDate對象直接調(diào)用now(),獲取到當(dāng)前日期,注意:如果使用我的實例,結(jié)果會不一樣,因為LocalDate.now()是調(diào)用時的日期 LocalDate currentDate = LocalDate.now(); //1.獲取年、月、日、星期幾、月中天、年中天、月數(shù) System.out.println("------------------1.獲取年、月、日、星期幾、月中天、年中天、月數(shù)-----------------------"); System.out.println("1.獲取到的當(dāng)前日期:" + currentDate); System.out.println("1.獲取到的年:" + currentDate.getYear()); System.out.println("1.獲取到的月:" + currentDate.getMonthValue()); System.out.println("1.獲取到的一月中的哪一天:" + currentDate.getDayOfMonth()); System.out.println("1.獲取到的一周中的星期幾:" + currentDate.getDayOfWeek().getValue()); System.out.println("1.獲取到的一年的第多少天:" + currentDate.getDayOfYear()); System.out.println("1.獲取到的一年有多少天:" + currentDate.lengthOfYear()); System.out.println("1.獲取到的一年有多少月:" + currentDate.lengthOfMonth()); //2.時間的計算 System.out.println("-----------------2.時間的計算,主要是加減法------------------------"); System.out.println("2.獲取到的當(dāng)前日期:" + currentDate); System.out.println("2.加法,當(dāng)前日期上加2年:" + currentDate.plusYears(2)); System.out.println("2.加法,當(dāng)前日期上加3個月:" + currentDate.plusMonths(3)); System.out.println("2.加法,當(dāng)前日期上加20天:" + currentDate.plusDays(20)); System.out.println("2.加法,當(dāng)前日期上加一周:" + currentDate.plusWeeks(1)); System.out.println("2.減法,當(dāng)前日期上減2年:" + currentDate.minusYears(2)); System.out.println("2.減法,當(dāng)前日期上減3個月:" + currentDate.minusMonths(3)); System.out.println("2.減法,當(dāng)前日期上減20天:" + currentDate.minusDays(20)); System.out.println("2.減法,當(dāng)前日期上減一周:" + currentDate.minusWeeks(1)); //3.時間的判斷及轉(zhuǎn)化 System.out.println("-----------------3.時間的判斷及格式化(格式化的類型很多)------------------------"); //新建一個格式化類型 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); //新創(chuàng)建一個LocalDate日期進行比較 LocalDate localDateOf = LocalDate.of(2020, 5, 20); System.out.println("3.當(dāng)前日期轉(zhuǎn)成yyyyMMdd型的字符串:" + currentDate.format(formatter)); System.out.println("3.當(dāng)前日期是否在一個日期之后:" + currentDate.isAfter(localDateOf)); System.out.println("3.當(dāng)前日期是否在一個日期之前:" + currentDate.isBefore(localDateOf)); System.out.println("3.當(dāng)前日期是否是閏年:" + currentDate.isLeapYear()); System.out.println("3.2020-05-20是否是閏年:" + localDateOf.isLeapYear()); //4.根據(jù)指定數(shù)據(jù)獲取日期或者時間(LocalTime) System.out.println("-----------------4.根據(jù)指定數(shù)據(jù)獲取日期或者時間(LocalTime)------------------------"); System.out.println("4.獲取到的當(dāng)前日期:" + currentDate); System.out.println("4.修改年數(shù)為1999年:" + currentDate.withYear(1999)); System.out.println("4.修改月數(shù)為10月:" + currentDate.withMonth(10)); System.out.println("4.修改天數(shù)為當(dāng)月12日:" + currentDate.withDayOfMonth(12)); System.out.println("4.獲取到的當(dāng)前日期的開始時間:" + currentDate.atStartOfDay()); System.out.println("4.根據(jù)指定的時、分、秒獲取時間:" + currentDate.atTime(12, 23, 45)); System.out.println("4.根據(jù)時間LocalTime對象獲取時間:" + currentDate.atTime(LocalTime.now())); }
運行結(jié)果:
------------------1.獲取年、月、日、星期幾、月中天、年中天、月數(shù)-----------------------
1.獲取到的當(dāng)前日期:2021-05-24
1.獲取到的年:2021
1.獲取到的月:5
1.獲取到的一月中的哪一天:24
1.獲取到的一周中的星期幾:1
1.獲取到的一年的第多少天:144
1.獲取到的一年有多少天:365
1.獲取到的一年有多少月:31
-----------------2.時間的計算,主要是加減法------------------------
2.獲取到的當(dāng)前日期:2021-05-24
2.加法,當(dāng)前日期上加2年:2023-05-24
2.加法,當(dāng)前日期上加3個月:2021-08-24
2.加法,當(dāng)前日期上加20天:2021-06-13
2.加法,當(dāng)前日期上加一周:2021-05-31
2.減法,當(dāng)前日期上減2年:2019-05-24
2.減法,當(dāng)前日期上減3個月:2021-02-24
2.減法,當(dāng)前日期上減20天:2021-05-04
2.減法,當(dāng)前日期上減一周:2021-05-17
-----------------3.時間的判斷及格式化(格式化的類型很多)------------------------
3.當(dāng)前日期轉(zhuǎn)成yyyyMMdd型的字符串:20210524
3.當(dāng)前日期是否在一個日期之后:true
3.當(dāng)前日期是否在一個日期之前:false
3.當(dāng)前日期是否是閏年:false
3.2020-05-20是否是閏年:true
-----------------4.根據(jù)指定數(shù)據(jù)獲取日期或者時間(LocalTime)------------------------
4.獲取到的當(dāng)前日期:2021-05-24
4.修改年數(shù)為1999年:1999-05-24
4.修改月數(shù)為10月:2021-10-24
4.修改天數(shù)為當(dāng)月12日:2021-05-12
4.獲取到的當(dāng)前日期的開始時間:2021-05-24T00:00
4.根據(jù)指定的時、分、秒獲取時間:2021-05-24T12:23:45
4.根據(jù)時間LocalTime對象獲取時間:2021-05-24T19:49:04.468
3.2 LocalTime的創(chuàng)建與使用
3.2.1、LocalTime創(chuàng)建
/** * LocalTime的初始化方法 * 此處單元測試的注解是采用:org.junit.Test */ @Test public void init() { //1.LocalTime,LocalTime對象直接調(diào)用now(),獲取到當(dāng)前時間 LocalTime now = LocalTime.now(); System.out.println("1.獲取到的當(dāng)前時間:" + now); //2.根據(jù)指定的時分秒生成時間 LocalTime localTimeOf = LocalTime.of(23, 59, 59); System.out.println("2.根據(jù)指定的時分秒生成時間:" + localTimeOf); //3.根據(jù)指定的時分秒生成時間 LocalTime localTimeParse = LocalTime.parse("12:23:45"); System.out.println("3.根據(jù)指定的時分秒生成時間:" + localTimeParse); //4.根據(jù)指定的時分秒和格式化類型生成時間 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmmss");//注意:HH是24小時制,hh是12小時制,時間類型的不要出現(xiàn)年月日如:yyyyMMdd LocalTime localTimeParseWithFormatter = LocalTime.parse("102008", formatter); System.out.println("4.根據(jù)指定的時分秒和格式化類型生成時間:" + localTimeParseWithFormatter); //5.根據(jù)時間對象TemporalAccessor生成 LocalTime initTime = LocalTime.of(11, 59, 59); System.out.println("5.根據(jù)時間對象TemporalAccessor生成:" + LocalTime.from(initTime)); }
運行結(jié)果:
1.獲取到的當(dāng)前時間:11:36:05.740
2.根據(jù)指定的時分秒生成時間:23:59:59
3.根據(jù)指定的時分秒生成時間:12:23:45
4.根據(jù)指定的時分秒和格式化類型生成時間:10:20:08
5.根據(jù)時間對象TemporalAccessor生成:11:59:59
3.2.2、LocalTime的常見使用方法
/** * LocalTime的常用使用方法 * 此處單元測試的注解是采用:org.junit.Test * 和LocalDate的操作方法差不多 */ @Test public void usage() { //此處采用LocalTime對象直接調(diào)用now(),獲取到當(dāng)前時間,注意:如果使用我的實例,結(jié)果會不一樣,因為LocalTime.now()是調(diào)用時的時間 LocalTime currentTime = LocalTime.now(); //1.獲取時、分、秒 System.out.println("------------------1.獲取時、分、秒-----------------------"); System.out.println("1.獲取到的當(dāng)前時間:" + currentTime); System.out.println("1.獲取當(dāng)前時間的小時:" + currentTime.getHour()); System.out.println("1.獲取當(dāng)前時間的分鐘:" + currentTime.getMinute()); System.out.println("1.獲取當(dāng)前時間的秒:" + currentTime.getSecond()); System.out.println("1.獲取當(dāng)前時間的秒:" + currentTime.getNano()); //2.時間的加減 System.out.println("------------------2.時間的加減-----------------------"); System.out.println("2.獲取到的當(dāng)前時間:" + currentTime); System.out.println("2.加法:當(dāng)前時間上增加1小時" + currentTime.plusHours(1)); System.out.println("2.加法:當(dāng)前時間上增加10分鐘" + currentTime.plusMinutes(10)); System.out.println("2.加法:當(dāng)前時間上增加20秒" + currentTime.plusSeconds(20)); System.out.println("2.減法:當(dāng)前時間上減加2小時" + currentTime.minusHours(2)); System.out.println("2.減法:當(dāng)前時間上減加30分鐘" + currentTime.minusMinutes(30)); System.out.println("2.減法:當(dāng)前時間上減加5秒" + currentTime.minusSeconds(5)); System.out.println("------------------3.時間的判斷及轉(zhuǎn)化-----------------------"); //初始化一個新的時間 LocalTime initTime = LocalTime.of(13, 59, 59); System.out.println("3.獲取到的當(dāng)前時間:" + currentTime); System.out.println("3.新初始化的時間:" + initTime); System.out.println("3.判斷當(dāng)前時間是否是一個時間之后:" + currentTime.isAfter(initTime)); System.out.println("3.判斷當(dāng)前時間是否是一個時間之前:" + currentTime.isBefore(initTime)); System.out.println("3.修改小時數(shù)為12:" + currentTime.withHour(12)); System.out.println("3.修改分鐘數(shù)為12:" + currentTime.withMinute(12)); System.out.println("3.修改秒數(shù)為12:" + currentTime.withSecond(12)); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); LocalTime parseTime = LocalTime.parse("11:45:28", formatter); System.out.println("3.根據(jù)指定的時分秒和格式化類型生成時間:" + parseTime); }
運行結(jié)果:
------------------1.獲取時、分、秒-----------------------
1.獲取到的當(dāng)前時間:19:50:14.612
1.獲取當(dāng)前時間的小時:19
1.獲取當(dāng)前時間的分鐘:50
1.獲取當(dāng)前時間的秒:14
1.獲取當(dāng)前時間的秒:612000000
------------------2.時間的加減-----------------------
2.獲取到的當(dāng)前時間:19:50:14.612
2.加法:當(dāng)前時間上增加1小時20:50:14.612
2.加法:當(dāng)前時間上增加10分鐘20:00:14.612
2.加法:當(dāng)前時間上增加20秒19:50:34.612
2.減法:當(dāng)前時間上減加2小時17:50:14.612
2.減法:當(dāng)前時間上減加30分鐘19:20:14.612
2.減法:當(dāng)前時間上減加5秒19:50:09.612
------------------3.時間的判斷及轉(zhuǎn)化-----------------------
3.獲取到的當(dāng)前時間:19:50:14.612
3.新初始化的時間:13:59:59
3.判斷當(dāng)前時間是否是一個時間之后:true
3.判斷當(dāng)前時間是否是一個時間之前:false
3.修改小時數(shù)為12:12:50:14.612
3.修改分鐘數(shù)為12:19:12:14.612
3.修改秒數(shù)為12:19:50:12.612
3.根據(jù)指定的時分秒和格式化類型生成時間:11:45:28
3.3 LocalDateTime的創(chuàng)建與使用
3.3.1、LocalDateTime創(chuàng)建
/** * LocalDateTime的初始化方法 * 此處單元測試的注解是采用:org.junit.Test */ @Test public void init() { //1.LocalDateTime對象直接調(diào)用now(),獲取到當(dāng)前時間 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("1.LocalDateTime對象直接調(diào)用now()獲取到的時間:" + localDateTime); //2.根據(jù)年月日時分秒構(gòu)造(此處方法比較多,不一一介紹) LocalDateTime localDateTimeOf = LocalDateTime.of(2021, 5, 10, 18, 30, 26); System.out.println("2.根據(jù)年月日時分秒構(gòu)造獲取到的時間:" + localDateTimeOf); //3.根據(jù)LocalDate和LocalTime得到(在有日期和時間的情況下可以使用) LocalDateTime of = LocalDateTime.of(LocalDate.now(), LocalTime.now()); System.out.println("3.根據(jù)LocalDate和LocalTime得到:" + of); //4.LocalDate指定一個LocalTime(LocalDate只有年月日) LocalTime localTimeInit = LocalTime.of(14, 25, 25); LocalDateTime localDateWithLocalTime = LocalDate.now().atTime(localTimeInit); System.out.println("4.LocalDate指定一個LocalTime:" + localDateWithLocalTime); //5.LocalTime指定一個LocalDate(LocalTime只有時分秒) LocalDate localDateInit = LocalDate.of(1998, 10, 1); LocalDateTime localTimeWithLocalDate = LocalTime.now().atDate(localDateInit); System.out.println("5.LocalTime指定一個LocalDate:" + localTimeWithLocalDate); }
運行結(jié)果:
1.LocalDateTime對象直接調(diào)用now()獲取到的時間:2021-05-24T19:51:15.237
2.根據(jù)年月日時分秒構(gòu)造獲取到的時間:2021-05-10T18:30:26
3.根據(jù)LocalDate和LocalTime得到:2021-05-24T19:51:15.238
4.LocalDate指定一個LocalTime:2021-05-24T14:25:25
5.LocalTime指定一個LocalDate:1998-10-01T19:51:15.238
3.3.2、LocalDateTime的常見使用方法
/** * LocalDateTime的常用使用方法 * 此處單元測試的注解是采用:org.junit.Test */ @Test public void usage() { //1.根據(jù)LocalDateTime獲取LocalDate LocalDateTime currentTime = LocalDateTime.now(); System.out.println("1.根據(jù)LocalDateTime獲取LocalDate: " + currentTime.toLocalDate()); //2.根據(jù)LocalDateTime獲取LocalTime System.out.println("2.根據(jù)LocalDateTime獲取LocalTime: " + currentTime.toLocalTime()); System.out.println("------------------3.時間的加減法及修改-----------------------"); //3.LocalDateTime的加減法包含了LocalDate和LocalTime的所有加減,上面說過,這里就只做簡單介紹 System.out.println("3.當(dāng)前時間:" + currentTime); System.out.println("3.當(dāng)前時間加5年:" + currentTime.plusYears(5)); System.out.println("3.當(dāng)前時間加2個月:" + currentTime.plusMonths(2)); System.out.println("3.當(dāng)前時間減2天:" + currentTime.minusDays(2)); System.out.println("3.當(dāng)前時間減5個小時:" + currentTime.minusHours(5)); System.out.println("3.當(dāng)前時間加5分鐘:" + currentTime.plusMinutes(5)); System.out.println("3.當(dāng)前時間加20秒:" + currentTime.plusSeconds(20)); //還可以靈活運用比如:向后加一年,向前減一天,向后加2個小時,向前減5分鐘,可以進行連寫 System.out.println("3.同時修改(向后加一年,向前減一天,向后加2個小時,向前減5分鐘):" + currentTime.plusYears(1).minusDays(1).plusHours(2).minusMinutes(5)); System.out.println("3.修改年為2025年:" + currentTime.withYear(2025)); System.out.println("3.修改月為12月:" + currentTime.withMonth(12)); System.out.println("3.修改日為27日:" + currentTime.withDayOfMonth(27)); System.out.println("3.修改小時為12:" + currentTime.withHour(12)); System.out.println("3.修改分鐘為12:" + currentTime.withMinute(12)); System.out.println("3.修改秒為12:" + currentTime.withSecond(12)); System.out.println("------------------4.時間的轉(zhuǎn)化及其他-----------------------"); //4.時間的格式化及其他 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime parse = LocalDateTime.parse("2020-09-18 14:55:44", formatter); System.out.println("4.時間字符串轉(zhuǎn)為時間:" + parse); LocalDate localDate = LocalDate.now(); System.out.println("4.所屬年份的第一天:" + localDate.with(firstDayOfYear())); System.out.println("4.所屬年份的最后一天:" + localDate.with(lastDayOfYear())); System.out.println("4.所屬年份的下一年的第一天:" + localDate.with(firstDayOfNextYear())); System.out.println("4.所屬月份的第一天:" + localDate.with(firstDayOfMonth())); System.out.println("4.所屬月份的最后一天:" + localDate.with(lastDayOfMonth())); System.out.println("4.所屬月份的下個月的第一天:" + localDate.with(firstDayOfNextMonth())); }
運行結(jié)果:
1.根據(jù)LocalDateTime獲取LocalDate: 2021-05-24
2.根據(jù)LocalDateTime獲取LocalTime: 19:57:46.316
------------------3.時間的加減法及修改-----------------------
3.當(dāng)前時間:2021-05-24T19:57:46.316
3.當(dāng)前時間加5年:2026-05-24T19:57:46.316
3.當(dāng)前時間加2個月:2021-07-24T19:57:46.316
3.當(dāng)前時間減2天:2021-05-22T19:57:46.316
3.當(dāng)前時間減5個小時:2021-05-24T14:57:46.316
3.當(dāng)前時間加5分鐘:2021-05-24T20:02:46.316
3.當(dāng)前時間加20秒:2021-05-24T19:58:06.316
3.同時修改(向后加一年,向前減一天,向后加2個小時,向前減5分鐘):2022-05-23T21:52:46.316
3.修改年為2025年:2025-05-24T19:57:46.316
3.修改月為12月:2021-12-24T19:57:46.316
3.修改日為27日:2021-05-27T19:57:46.316
3.修改小時為12:2021-05-24T12:57:46.316
3.修改分鐘為12:2021-05-24T19:12:46.316
3.修改秒為12:2021-05-24T19:57:12.316
------------------4.時間的轉(zhuǎn)化及其他-----------------------
4.時間字符串轉(zhuǎn)為時間:2020-09-18T14:55:44
4.所屬年份的第一天:2021-01-01
4.所屬年份的最后一天:2021-12-31
4.所屬年份的下一年的第一天:2022-01-01
4.所屬月份的第一天:2021-05-01
4.所屬月份的最后一天:2021-05-31
4.所屬月份的下個月的第一天:2021-06-01
到此這篇關(guān)于Java中LocalDateTime的具體用法的文章就介紹到這了,更多相關(guān)Java LocalDateTime內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

Java中反射的"暴破"機制(SetAccessible方法)詳解

Struts2+Hibernate實現(xiàn)數(shù)據(jù)分頁的方法

SpringBoot Shiro 權(quán)限注解不起作用的解決方法