LocalDateTime的时间工具类常用方法整理

LocalDateTime 的时间工具类常用方法整理

LocalDateTime 是 Java 8 引入的日期时间 API 中的一个重要类,它提供了一种灵活的方式来表示和操作日期和时间。在处理日期和时间相关的任务时,掌握 LocalDateTime 的各种方法和用法至关重要。梳理 LocalDateTime 中一些常用的方法,帮助您更好地理解和使用这个强大的时间工具类。

基本概念

LocalDateTime 是一个不可变的日期时间对象,它可以表示从 1970 年 1 月 1 日(UTC)以来的秒数。LocalDateTime 可以用于表示当前时间、指定时间点以及计算时间间隔等。

常用方法

now(): 获取当前的 LocalDateTime 对象。minusMinutes(int m): 获取当前时间的过去 m 分钟的 LocalDateTime 对象。plusMinutes(int m): 获取当前时间的未来 m 分钟的 LocalDateTime 对象。plusHours(int h): 获取当前时间的将来 h 小时的 LocalDateTime 对象。minusHours(int h): 获取当前时间的过去 h 小时的 LocalDateTime 对象。plusDays(int d): 获取当前时间的将来 d 天之后的 LocalDateTime 对象。minusDays(int d): 获取当前时间的过去 d 天的 LocalDateTime 对象。plusMonths(int m): 获取当前时间的将来 m 个月之后的 LocalDateTime 对象。minusMonths(int m): 获取当前时间的过去 m 个月的 LocalDateTime 对象。plusYears(int y): 获取当前时间的将来 y 年后的 LocalDateTime 对象。minusYears(int y): 获取当前时间的过去 y 年的 LocalDateTime 对象。plusWeeks(int w): 获取当前时间的将来 w 周之后的 LocalDateTime 对象。minusWeeks(int w): 获取当前时间的过去 w 周的 LocalDateTime 对象。plusDaysOfMonth(int d): 获取当前时间的将来第 d 天的 LocalDateTime 对象。minusDaysOfMonth(int d): 获取当前时间的过去第 d 天的 LocalDateTime 对象。plusDaysOfMonth(int d, int m): 获取当前时间的将来第 d 天的每月第 m 天的 LocalDateTime 对象。minusDaysOfMonth(int d, int m): 获取当前时间的过去第 d 天的每月第 m 天的 LocalDateTime 对象。plusDaysOfMonth(int d, int m, int y): 获取当前时间的将来第 d 天的每月第 m 月的第 y 年的 LocalDateTime 对象。minusDaysOfMonth(int d, int m, int y): 获取当前时间的过去第 d 天的每月第 m 月的第 y 年的 LocalDateTime 对象。plusDaysOfMonth(int d, int m, int y, int w): 获取当前时间的将来第 d 天的每月第 m 月的第 y 年的每周第 w 天的 LocalDateTime 对象。minusDaysOfMonth(int d, int m, int y, int w): 获取当前时间的过去第 d 天的每月第 m 月的第 y 年的每周第 w 天的 LocalDateTime 对象。plusDaysOfMonth(int d, int m, int y, int w, int h): 获取当前时间的将来第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 天的 LocalDateTime 对象。minusDaysOfMonth(int d, int m, int y, int w, int h): 获取当前时间的过去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 天的 LocalDateTime 对象。plusDaysOfMonth(int d, int m, int y, int w, int h, int a): 获取当前时间的将来第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的 LocalDateTime 对象。minusDaysOfMonth(int d, int m, int y, int w, int h, int a): 获取当前时间的过去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的 LocalDateTime 对象。plusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b): 获取当前时间的将来第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的 LocalDateTime 对象。minusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b): 获取当前时间的过去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的 LocalDateTime 对象。plusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c): 获取当前时间的将来第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的 LocalDateTime 对象。minusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c): 获取当前时间的过去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的 LocalDateTime 对象。plusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d): 获取当前时间的将来第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的 LocalDateTime 对象。minusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d): 获取当前时间的过去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的 LocalDateTime 对象。plusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d, int e): 获取当前时间的将来第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的每月第 e 日的 LocalDateTime 对象。minusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d, int e): 获取当前时间的过去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的每月第 e 日的 LocalDateTime 对象。plusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d, int e, int f): 获取当前时间的将来第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的每月第 e 日的每月第 f 日的 LocalDateTime 对象。minusDaysOfMonth(int d, int m, int y, int w, int h, int a, int b, int c, int d, int e, f): 获取当前时间的过去第 d 天的每月第 m 月的第 y 年的每周第 w 月的第 h 月的第 a 天的每月第 b 日的每月第 c 日的每月第 d 日的每月第 e 日的每月第 f 日的 LocalDateTime 对象。

通过以上方法,您可以根据需要轻松地创建、查询和操作 LocalDateTime 对象,从而满足各种日期和时间相关的任务需求。熟练掌握这些方法将有助于您更高效地处理与日期和时间相关的编程任务。

na.png

本网站文章未经允许禁止转载,合作/权益/投稿 请联系平台管理员 Email:epebiz@outlook.com