您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java8的LocalDateTime怎么使用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java8的LocalDateTime怎么使用”吧!
LocalDateTime、LocalDate、LocalTime 是 Java8 全新的日期框架,加強(qiáng)了對(duì)時(shí)間的管理,有很多特別好用的時(shí)間運(yùn)算方法,而且是線程安全的,較之前的 util.Date 以及 Calander 使用起來更加的方便直觀,下面介紹幾種常見的日期對(duì)象用法。
LocalDateTime:日期加時(shí)間的日期對(duì)象,包含年月日時(shí)分秒
LocalDate:日期類,包含年月日
LocalTime:時(shí)間類,包含時(shí)分秒
@Test public void test() { LocalDate localDate = LocalDate.now(); LocalTime localTime = LocalTime.now(); LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("localDate:"+localDate);//2023-02-22 System.out.println("localTime:"+localTime);//17:25:36.590 System.out.println("localDateTime:"+localDateTime);//2023-02-22T17:25:36.590 }
輸出結(jié)果:
可以看到不用做格式轉(zhuǎn)換就可以得到可讀性很高的日期格式。注意:ISO 8601規(guī)定的日期和時(shí)間分隔符是T。標(biāo)準(zhǔn)格式如下:
日期:yyyy-MM-dd
時(shí)間:HH:mm:ss
帶毫秒的時(shí)間:HH:mm:ss.SSS
日期和時(shí)間:yyyy-MM-dd'T'HH:mm:ss
帶毫秒的日期和時(shí)間:yyyy-MM-dd'T'HH:mm:ss.SSS
@Test public void test() { LocalDateTime localDateTime = LocalDateTime.now(); // 獲取當(dāng)前時(shí)間 int year = localDateTime.getYear(); // 獲取年份 2023 int month = localDateTime.getMonthValue(); // 獲取月份 2 int day = localDateTime.getDayOfMonth(); // 獲取月中的天數(shù) 22 int hour = localDateTime.getHour(); // 獲取當(dāng)前的小時(shí) 17 int minute = localDateTime.getMinute(); // 獲取當(dāng)前分鐘 33 int second = localDateTime.getSecond(); // 獲取當(dāng)前秒數(shù) 22 System.out.println(year); System.out.println(month); System.out.println(day); System.out.println(hour); System.out.println(minute); System.out.println(second); }
輸出結(jié)果:
在static目錄中新建kaptcha.html頁面,代碼如下:
public void test() { LocalDateTime of = LocalDateTime.of(2023,2,22,22,22,22); System.out.println(of); // 輸出2023-02-22T22:22:22 }
@Test public void test() { // 將字符串轉(zhuǎn)換為指定格式的時(shí)間(格式要和給定的格式一致,不然會(huì)報(bào)錯(cuò)) LocalDateTime parse = LocalDateTime.parse("2023-02-22 22:22:22", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); LocalDateTime parse1 = LocalDateTime.parse("2023 02 22 22:22:22", DateTimeFormatter.ofPattern("yyyy MM dd HH:mm:ss")); LocalDateTime parse2 = LocalDateTime.parse("2023.02.22 22:22:22", DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss")); System.out.println(parse); // 輸出2023-02-22T22:22:22 System.out.println(parse1); // 輸出2023-02-22T22:22:22 System.out.println(parse2); // 輸出2023-02-22T22:22:22 // 時(shí)間轉(zhuǎn)字符串 LocalDateTime now = LocalDateTime.now(); DateTimeFormatter of = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String dateTime = now.format(of); System.out.println(dateTime); // 輸出 2023-02-22 17:56:18 }
輸出結(jié)果:
LocalDateTime提供了對(duì)日期和時(shí)間進(jìn)行加減的非常簡單的鏈?zhǔn)秸{(diào)用,讓時(shí)間運(yùn)算變得非常簡單:
@Test public void test() { LocalDateTime now = LocalDateTime.now(); // 當(dāng)前時(shí)間2023-02-22T18:00:19.352 LocalDateTime plusYears= now.plusYears(2); // 在當(dāng)前時(shí)間加上2年2025-02-22T18:00:19.352 LocalDateTime plusMonths= now.plusMonths(2);// 在當(dāng)前時(shí)間商加上2月2023-04-22T18:00:19.352 LocalDateTime plusDays= now.plusDays(2); // 在當(dāng)前時(shí)間加上2天2023-02-24T18:00:19.352 LocalDateTime plusHours= now.plusHours(2); // 在當(dāng)前時(shí)間加上2個(gè)小時(shí)2023-02-22T20:00:19.352 LocalDateTime plusMinutes= now.plusMinutes(30); // 在當(dāng)前時(shí)間加上30分鐘2023-02-22T18:30:19.352 LocalDateTime plusSeconds = now.plusSeconds(30); // 在當(dāng)前時(shí)間加上30秒2023-02-22T18:00:49.352 System.out.println(now); System.out.println(plusYears); System.out.println(plusMonths); System.out.println(plusDays); System.out.println(plusHours); System.out.println(plusMinutes); System.out.println(plusSeconds); }
輸出結(jié)果:
@Test public void test() { LocalDateTime now = LocalDateTime.now(); // 當(dāng)前時(shí)間 LocalDateTime minusYears = now.minusYears(2); // 在當(dāng)前時(shí)間減上2年 LocalDateTime minusMonths = now.minusMonths(2);// 在當(dāng)前時(shí)間商減上2月 LocalDateTime minusDays = now.minusDays(2); // 在當(dāng)前時(shí)間減上2天 LocalDateTime minusHours = now.minusHours(2); // 在當(dāng)前時(shí)間減上2個(gè)小時(shí) LocalDateTime minusMinutes = now.minusMinutes(30); // 在當(dāng)前時(shí)間減上30分鐘 LocalDateTime minusSeconds = now.minusSeconds(30); // 在當(dāng)前時(shí)間減上30秒 System.out.println("now:" + now); System.out.println("minusYears:" + minusYears); System.out.println("minusMonths:" + minusMonths); System.out.println("minusDays:" + minusDays); System.out.println("minusHours:" + minusHours); System.out.println("minusMinutes:" + minusMinutes); System.out.println("minusSeconds:" + minusSeconds); }
輸出結(jié)果:
@Test public void test() { LocalDateTime now = LocalDateTime.now(); // 當(dāng)前時(shí)間 LocalDateTime now1 = now.plusYears(5); // 在當(dāng)前時(shí)間加上5年 // 給LocalDateTime 賦值 LocalDateTime of = LocalDateTime.of(2023,2,2,22,22,22); LocalDateTime of1 = LocalDateTime.of(2023,8,5,1,1,1); //兩個(gè)時(shí)間作比較,第一個(gè)時(shí)間減去第二個(gè)時(shí)間(如果年份相同,比較月份,月份相同比較天數(shù),以此類推) int compareTo = now1.compareTo(now); int compareTo1 = now.compareTo(now1); int compareTo2 = now.compareTo(of); int compareTo3 = now.compareTo(of1); System.out.println(now); // 輸出 2023-02-22T20:19:44.112v System.out.println(now1); // 輸出 2028-02-22T20:19:44.112 System.out.println(of); // 輸出 2023-02-02T22:22:22 System.out.println(of1); // 輸出 2023-08-05T01:01:01 System.out.println(compareTo); // 輸出 5 System.out.println(compareTo1); // 輸出 -5 System.out.println(compareTo2); // 輸出 20 System.out.println(compareTo3); // 輸出 -6 }
輸出結(jié)果:
注意沒有計(jì)算相差的年和秒值,對(duì)于要計(jì)算相差的秒數(shù),可以利用計(jì)算毫秒來進(jìn)行轉(zhuǎn)換
@Test public void test() { LocalDateTime now = LocalDateTime.now(); // 當(dāng)前時(shí)間 // 給LocalDateTime 賦值 LocalDateTime of = LocalDateTime.of(2022,2,22,2,2,2); Duration duration = Duration.between(of,now); // 后面減去前面 long toDays = Duration.between(of,now).toDays(); //相差的天數(shù) long toHours = Duration.between(of,now).toHours();//相差的小時(shí)數(shù) long toMinutes = Duration.between(of,now).toMinutes();//相差的分鐘數(shù) long toMillis = Duration.between(of,now).toMillis();//相差毫秒數(shù) long toNanos = Duration.between(of,now).toNanos();//相差的納秒數(shù) System.out.println("toDays:"+ toDays); // 輸出 toDays:365 System.out.println("toHours:"+ toHours); // 輸出 toHours:8778 System.out.println("toMinutes:"+ toMinutes); // 輸出 toMinutes:526732 System.out.println("toMillis:"+ toMillis); // 輸出 toMillis:31603973840 System.out.println("toNanos:"+ toNanos); // 輸出 toNanos:31603973840000000 }
輸出結(jié)果:
@Test public void test() { LocalDateTime now = LocalDateTime.now(); // 當(dāng)前時(shí)間 LocalDateTime withYear = now.withYear(2060); // 改變當(dāng)前年份(變成2060年) LocalDateTime withMonth = now.withMonth(12); // 改變當(dāng)前月份(變成12月份) LocalDateTime withDayOfMonth = now.withDayOfMonth(28); //改變當(dāng)前天數(shù)(變成28日) LocalDateTime withHour = now.withHour(23); // 改變當(dāng)前小時(shí)數(shù)(變成23時(shí)) LocalDateTime withMinute = now.withMinute(30); // 改變當(dāng)前分鐘(變成30分鐘) LocalDateTime withSecond = now.withSecond(23); // 改變當(dāng)前小時(shí)數(shù)(變成23時(shí)) LocalDateTime withDayOfYear = now.withDayOfYear(60); // 從一月一號(hào)開始加上60天 System.out.println(now); System.out.println("withYear:"+ withYear); System.out.println("withMonth:"+ withMonth); System.out.println("withDayOfMonth:"+ withDayOfMonth); System.out.println("withHour:"+ withHour); System.out.println("withMinute:"+ withMinute); System.out.println("withSecond:"+ withSecond); System.out.println("withDayOfYear:"+ withDayOfYear); }
輸出結(jié)果:
@Test public void test() { // 自定義格式化: DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss"); System.out.println("自定義格式y(tǒng)yyy/MM/dd HH:mm:ss :"+dtf.format(LocalDateTime.now())); System.out.println("自定義格式y(tǒng)yyy.MM.dd HH:mm:ss :"+dtf1.format(LocalDateTime.now())); // 用自定義格式解析: LocalDateTime dt2 = LocalDateTime.parse("2020/10/20 15:16:17", dtf); System.out.println("格式解析:"+dt2); }
輸出結(jié)果:
@Test public void test() { // 本月第一天0:00時(shí)刻: LocalDateTime firstDay = LocalDate.now().withDayOfMonth(1).atStartOfDay(); // 本月最后1天: LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()); // 下月第1天: LocalDate nextMonthFirstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth()); // 本月第1個(gè)周一: LocalDate firstWeekday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); }
@Test public void test() { //判斷兩個(gè)時(shí)間點(diǎn)的前后 LocalDateTime now = LocalDateTime.now(); LocalDateTime target = LocalDateTime.of(2022, 2, 22, 22, 22, 22); boolean isBefore = now.isBefore(target); System.out.println("now:"+now); System.out.println("target:"+target); System.out.println("isBefore:"+isBefore); System.out.println(LocalDate.now().isBefore(LocalDate.of(2022, 2, 22))); System.out.println(LocalTime.now().isAfter(LocalTime.parse("08:15:00"))); }
輸出結(jié)果:
感謝各位的閱讀,以上就是“Java8的LocalDateTime怎么使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Java8的LocalDateTime怎么使用這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。