LocalDateTime 的時(shí)間工具類常用方法整理
Shoprite優(yōu)選生活館軟件工具2025-07-139490
LocalDateTime 是 Java 8 引入的日期時(shí)間 API 中的一個(gè)重要類,它提供了一種靈活的方式來表示和操作日期和時(shí)間。在處理日期和時(shí)間相關(guān)的任務(wù)時(shí),掌握 LocalDateTime 的各種方法和用法至關(guān)重要。梳理 LocalDateTime 中一些常用的方法,幫助您更好地理解和使用這個(gè)強(qiáng)大的時(shí)間工具類。
基本概念
LocalDateTime 是一個(gè)不可變的日期時(shí)間對(duì)象,它可以表示從 1970 年 1 月 1 日(UTC)以來的秒數(shù)。LocalDateTime 可以用于表示當(dāng)前時(shí)間、指定時(shí)間點(diǎn)以及計(jì)算時(shí)間間隔等。
常用方法
- now(): 獲取當(dāng)前的 LocalDateTime 對(duì)象。
- minusMinutes(int m): 獲取當(dāng)前時(shí)間的過去 m 分鐘的 LocalDateTime 對(duì)象。
- plusMinutes(int m): 獲取當(dāng)前時(shí)間的未來 m 分鐘的 LocalDateTime 對(duì)象。
- plusHours(int h): 獲取當(dāng)前時(shí)間的將來 h 小時(shí)的 LocalDateTime 對(duì)象。
- minusHours(int h): 獲取當(dāng)前時(shí)間的過去 h 小時(shí)的 LocalDateTime 對(duì)象。
- plusDays(int d): 獲取當(dāng)前時(shí)間的將來 d 天之后的 LocalDateTime 對(duì)象。
- minusDays(int d): 獲取當(dāng)前時(shí)間的過去 d 天的 LocalDateTime 對(duì)象。
- plusMonths(int m): 獲取當(dāng)前時(shí)間的將來 m 個(gè)月之后的 LocalDateTime 對(duì)象。
- minusMonths(int m): 獲取當(dāng)前時(shí)間的過去 m 個(gè)月的 LocalDateTime 對(duì)象。
- plusYears(int y): 獲取當(dāng)前時(shí)間的將來 y 年后的 LocalDateTime 對(duì)象。
- minusYears(int y): 獲取當(dāng)前時(shí)間的過去 y 年的 LocalDateTime 對(duì)象。
- plusWeeks(int w): 獲取當(dāng)前時(shí)間的將來 w 周之后的 LocalDateTime 對(duì)象。
- minusWeeks(int w): 獲取當(dāng)前時(shí)間的過去 w 周的 LocalDateTime 對(duì)象。
- plusDaysOfMonth(int d): 獲取當(dāng)前時(shí)間的將來第 d 天的 LocalDateTime 對(duì)象。
- minusDaysOfMonth(int d): 獲取當(dāng)前時(shí)間的過去第 d 天的 LocalDateTime 對(duì)象。
- plusDaysOfMonth(int d, int m): 獲取當(dāng)前時(shí)間的將來第 d 天的每月第 m 天的 LocalDateTime 對(duì)象。
- minusDaysOfMonth(int d, int m): 獲取當(dāng)前時(shí)間的過去第 d 天的每月第 m 天的 LocalDateTime 對(duì)象。
- plusDaysOfMonth(int d, int m, int y): 獲取當(dāng)前時(shí)間的將來第 d 天的每月第 m 月的第 y 年的 LocalDateTime 對(duì)象。
- minusDaysOfMonth(int d, int m, int y): 獲取當(dāng)前時(shí)間的過去第 d 天的每月第 m 月的第 y 年的 LocalDateTime 對(duì)象。
- plusDaysOfMonth(int d, int m, int y, int w): 獲取當(dāng)前時(shí)間的將來第 d 天的每月第 m 月的第 y 年的每周第 w 天的 LocalDateTime 對(duì)象。
- minusDaysOfMonth(int d, int m, int y, int w): 獲取當(dāng)前時(shí)間的過去第 d 天的每月第 m 月的第 y 年的每周第 w 天的 LocalDateTime 對(duì)象。
- plusDaysOfMonth(int d, int m, int y, int w, int h): 獲取當(dāng)前時(shí)間的將來第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 天的 LocalDateTime 對(duì)象。
- minusDaysOfMonth(int d, int m, int y, int w, int h): 獲取當(dāng)前時(shí)間的過去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 天的 LocalDateTime 對(duì)象。
- plusDaysOfMonth(int d, int m, int y, int w, int h, int a): 獲取當(dāng)前時(shí)間的將來第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的 LocalDateTime 對(duì)象。
- minusDaysOfMonth(int d, int m, int y, int w, int h, int a): 獲取當(dāng)前時(shí)間的過去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的 LocalDateTime 對(duì)象。
- plusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b): 獲取當(dāng)前時(shí)間的將來第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的 LocalDateTime 對(duì)象。
- minusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b): 獲取當(dāng)前時(shí)間的過去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的 LocalDateTime 對(duì)象。
- plusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c): 獲取當(dāng)前時(shí)間的將來第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的 LocalDateTime 對(duì)象。
- minusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c): 獲取當(dāng)前時(shí)間的過去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的 LocalDateTime 對(duì)象。
- plusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d): 獲取當(dāng)前時(shí)間的將來第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的 LocalDateTime 對(duì)象。
- minusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d): 獲取當(dāng)前時(shí)間的過去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的 LocalDateTime 對(duì)象。
- plusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d, int e): 獲取當(dāng)前時(shí)間的將來第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的每月第 e 日的 LocalDateTime 對(duì)象。
- minusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d, int e): 獲取當(dāng)前時(shí)間的過去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的每月第 e 日的 LocalDateTime 對(duì)象。
- plusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d, int e, int f): 獲取當(dāng)前時(shí)間的將來第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的每月第 e 日的每月第 f 日的 LocalDateTime 對(duì)象。
- minusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d, int e, f): 獲取當(dāng)前時(shí)間的過去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的每月第 e 日的每月第 f 日的 LocalDateTime 對(duì)象。
通過以上方法,您可以根據(jù)需要輕松地創(chuàng)建、查詢和操作 LocalDateTime 對(duì)象,從而滿足各種日期和時(shí)間相關(guān)的任務(wù)需求。熟練掌握這些方法將有助于您更高效地處理與日期和時(shí)間相關(guān)的編程任務(wù)。
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。