java.time.LocalDateTime 类
在本教程中,您将学习如何使用java.time.LocalDateTime类,简介java.time.LocalDateTime类表示ISO-8601日历系统中没有时区的日期时间,例如2007-12-03T10:15:30。
java.time.LocalDateTime 类表示 ISO-8601 日历系统中没有时区的日期时间,例如 2007-12-03T10:15:30。
类声明
以下是 java.time.LocalDateTime 类的声明 −
public final class LocalDateTime extends Object implements Temporal, TemporalAdjuster, ChronoLocalDateTime, Serializable
运行实例 »
点击 "运行实例" 按钮查看在线实例
字段
以下是 java.time.LocalDateTime 类的字段 −
static LocalDateTime MAX − 支持的最大 LocalDateTime,'+999999999-12-31T23:59:59.999999999'。
static LocalDateTime MIN − 支持的最小 LocalDateTime,'-999999999-01-01T00:00:00'。
类方法
序号 | 方法 & 描述 |
---|---|
1 | Temporal adjustInto(Temporal temporal)
将指定的时间对象调整为与该对象具有相同的日期和时间。 |
2 | OffsetDateTime atOffset(ZoneOffset offset)
将此日期时间与偏移时间组合以创建 OffsetDateTime。 |
3 | ZonedDateTime atZone(ZoneId zone)
将此日期时间与时区组合以创建 ZonedDateTime。 |
4 | int compareTo(ChronoLocalDateTime<?> other)
将此日期时间与另一个日期时间进行比较。 |
5 | boolean equals(Object obj)
检查此日期时间是否等于另一个日期时间。 |
6 | String format(DateTimeFormatter formatter)
使用指定的格式化程序格式化此日期时间。 |
7 | static LocalDateTime from(TemporalAccessor temporal)
从时间对象获取 LocalDateTime 的实例。 |
8 | int get(TemporalField field)
从此日期时间获取指定字段的值作为 int。 |
9 | int getDayOfMonth()
获取日期字段。 |
10 | DayOfWeek getDayOfWeek()
获取星期字段,它是一个枚举 DayOfWeek。 |
11 | int getDayOfYear()
获取日期字段。 |
12 | int getHour()
获取小时字段。 |
13 | long getLong(TemporalField field)
从此日期时间获取指定字段的值作为long。 |
14 | Month getMinute()
获取分钟字段。 |
15 | Month getMonth()
使用 Month 枚举获取月份字段。 |
16 | int getMonthValue()
获取从 1 到 12 的年份字段。 |
17 | int getNano()
获取纳秒字段。 |
18 | int getSecond()
获取秒字段。 |
19 | int getYear()
获取年份字段。 |
20 | int hashCode()
此日期时间的哈希码。 |
21 | boolean isAfter(ChronoLocalDateTime<?> other)
检查此日期时间是否在指定的日期时间之后。 |
22 | boolean isBefore(ChronoLocalDateTime<?> other)
检查此日期时间是否早于指定的日期时间。 |
23 | boolean isEqual(ChronoLocalDateTime<?> other)
检查此日期时间是否等于指定的日期时间。 |
24 | boolean isSupported(TemporalField field)
检查是否支持指定的字段。 |
25 | boolean isSupported(TemporalUnit unit)
检查是否支持指定的单位。 |
26 | LocalDateTime减(long amountToSubtract, TemporalUnit unit)
返回此日期时间的副本,减去指定的数量。 |
27 | LocalDateTime minus(TemporalAmount amountToSubtract)
返回减去指定数量的此日期时间的副本。 |
28 | LocalDateTime minusDays(long daysToSubtract)
返回此 LocalDateTime 的副本,减去指定的天数。 |
29 | LocalDateTime minusHours(long hoursToSubtract)
返回此 LocalDateTime 的副本,减去指定的小时数。 |
30 | LocalDateTime minusMinutes(long minutesToSubtract)
返回此 LocalDateTime 的副本,减去指定的分钟数。 |
31 | LocalDateTime minusMonths(long monthsToSubtract)
返回此 LocalDateTime 的副本,减去指定的月数。 |
32 | LocalDateTime minusNanos(long nanos)
返回此 LocalDateTime 的副本,减去指定的纳秒数。 |
33 | LocalDateTime minusSeconds(long seconds)
返回此 LocalDateTime 的副本,减去指定的秒数。 |
34 | LocalDateTime minusWeeks(long weekToSubtract)
返回此 LocalDateTime 的副本,减去指定的周数。 |
35 | LocalDateTime minusYears(long yearsToSubtract)
返回此 LocalDateTime 的副本,减去指定的年数。 |
36 | static LocalDateTime now()
从默认时区的系统时钟获取当前日期时间。 |
37 | static LocalDateTime now(Clock clock)
从指定时钟获取当前日期时间。 |
38 | static LocalDateTime now(ZoneId zone)
从指定时区的系统时钟获取当前日期时间。 |
39 | static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)
从年、月、日、时、分获取LocalDateTime的实例,将秒和纳秒设置为零。 |
40 | static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)
从年、月、日、小时、分钟和秒中获取LocalDateTime 的实例,将纳秒设置为零。 |
41 | static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
从年、月、日、时、分、秒、纳秒中获取LocalDateTime的实例。 |
42 | static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second)
从年、月、日、小时、分钟和秒中获取LocalDateTime 的实例,将纳秒设置为零。 |
43 | static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
从年、月、日、时、分、秒、纳秒中获取LocalDateTime的实例。 |
44 | static LocalDateTime of(LocalDate date, LocalTime time)
从日期和时间获取 LocalDateTime 的实例。 |
45 | static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset)
从 1970-01-01T00:00:00Z 的 epoch 获取 LocalDateTime 的实例。 |
46 | static LocalDateTime ofInstant(Instant instant, ZoneId zone)
从 Instant 和区域 ID 获取 LocalDateTime 的实例。 |
47 | static LocalDateTime parse(CharSequence text)
从 2007-12-03T10:15:30 等文本字符串中获取 LocalDateTime 的实例。 |
48 | static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)
使用特定格式化程序从文本字符串中获取 LocalDateTime 的实例。 |
49 | LocalDateTime plus(long amountToAdd, TemporalUnit unit)
返回此日期时间的副本,并添加指定的数量。 |
50 | LocalDateTime plus(TemporalAmount amountToAdd)
返回此日期时间的副本,其中添加了指定的数量。 |
51 | LocalDateTime plusDays(long daysToAdd)
返回此 LocalDateTime 的副本,并添加指定的天数。 |
52 | LocalDateTime plusHours(long hoursToAdd)
返回此 LocalDateTime 的副本,并添加指定的小时数。 |
53 | LocalDateTime plusMinutes(long minutesToAdd)
返回此 LocalDateTime 的副本,并添加指定的分钟数。 |
54 | LocalDateTime plusMonths(long monthsToAdd)
返回此 LocalDateTime 的副本,并添加指定的月数。 |
55 | LocalDateTime plusNanos(long nanos)
返回此 LocalDateTime 的副本,并添加指定的纳秒数。 |
56 | LocalDateTime plusSeconds(long seconds)
返回此 LocalDateTime 的副本,并添加指定的秒数。 |
57 | LocalDateTime plusWeeks(long weeksToAdd)
返回此 LocalDateTime 的副本,并添加指定的周数。 |
58 | LocalDateTime plusYears(long yearsToAdd)
返回此 LocalDateTime 的副本,并添加指定的年数。 |
59 | <R> R query(TemporalQuery<R> query)
使用指定的查询查询此日期时间。 |
60 | ValueRange range(TemporalField field)
获取指定字段的有效值范围。 |
61 | LocalDate toLocalDate()
获取此日期时间的 LocalDate 部分。 |
62 | LocalTime toLocalTime()
获取此日期时间的 LocalTime 部分。 |
63 | String toString()
将此日期输出为字符串,例如 2007-12-03T10:15:30。 |
64 | LocalDateTime truncatedTo(TemporalUnit unit)
返回此 LocalDateTime 的副本,其中时间被截断。 |
65 | long until(Temporal endExclusive, TemporalUnit unit)
以指定单位计算距离另一个日期时间的时间量。 |
66 | LocalDateTime with(TemporalAdjuster adjuster)
返回此日期时间的调整副本。 |
67 | LocalDateTime with(TemporalField field, long newValue)
返回此日期时间的副本,并将指定字段设置为新值。 |
68 | LocalDateTime withDayOfMonth(int dayOfMonth)
返回此 LocalDateTime 的副本,其中日期已更改。 |
69 | LocalDateTime withDayOfYear(int dayOfYear)
返回此 LocalDateTime 的副本,其中日期已更改。 |
70 | LocalDateTime withHour(int hour)
返回此 LocalDateTime 的副本,其中更改了一天中的时间。 |
71 | LocalDateTime withMinute(int minute)
返回此 LocalDateTime 的副本,其中分钟已更改。 |
72 | LocalDateTime withMonth(int month)
返回此 LocalDateTime 的副本,其中更改了年份。 |
73 | LocalDateTime withNano(int nanoOfSecond)
返回此 LocalDateTime 的副本,其中更改了纳秒。 |
74 | LocalDateTime withSecond(int second)
返回此 LocalDateTime 的副本,其中秒数已更改。 |
75 | LocalDateTime withYear(int year)
返回此 LocalDateTime 的副本,其中年份已更改。 |
继承的方法
这个类继承了以下类的方法 −
- Java.lang.Object