五、LocalDateTime类
java.time.LocalDateTime 是一个不可变的日期时间对象,代表日期时间。
public final class LocalDateTime
implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>,
Serializable {
1.字段
在 java.time.LocalDateTime 类中声明了两个实例字段用来存储 日期 和 时间 :
/**
* The date part.
*/
private final LocalDate date;
/**
* The time part.
*/
private final LocalTime time;
可以看出其实 LocalDateTime 底层就是一个 LocalDate + LocalTime 。
这里需要注意,不仅仅 date 和 time 字段是 final 修饰的, LocalDate 、 LocalTime类中的实例字段也是 final 修饰的,因此 LocalDateTime 的实例一经创建,其内部的各项取值都是不可更改的。
2.获取LocalDateTime实例
public static LocalDateTime now();
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int
minute);
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int
minute, int second);
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int
minute, int second, int nanoOfSecond);
public static LocalDateTime of(LocalDate date, LocalTime time);
示例:
public static void main(String[] args) {
LocalDateTime datetime = LocalDateTime.now();
System.out.println(datetime);
LocalDateTime first = LocalDateTime.of(1995, 7, 4, 7, 30);
System.out.println(first);//1995-07-04T07:30
LocalDateTime third = LocalDateTime.of(1999, 11, 20, 6, 30, 7, 0);
System.out.println(third);//1999-11-20T06:30:07
//LocalDate\LocalTime 转换成LocalDateTime
LocalDate date = LocalDate.of(1997, 7, 1);
LocalTime time = LocalTime.of(10, 20);
LocalDateTime fourth = LocalDateTime.of(date, time);
System.out.println(fourth);//1997-07-01T10:20
}
3.方法
now() | LocalDateTime | 返回当前日期和时间。 |
---|---|---|
of(int year, int month, int dayOfMonth, int hour, int minute) | LocalDateTime | 使用给定的年、月、日、小时和分钟创建一个 LocalDateTime 对象。 |
of(int year, int month, int dayOfMonth, int hour, int minute, int second) | LocalDateTime | 使用给定的年、月、日、小时、分钟和秒创建一个 LocalDateTime 对象。 |
of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) | LocalDateTime | 使用给定的年、月、日、小时、分钟、秒和纳秒创建一个 LocalDateTime 对象。 |
of(LocalDate date, LocalTime time) | LocalDateTime | 使用给定的日期和时间创建一个 LocalDateTime 对象。 |
parse(CharSequence text) | LocalDateTime | 从字符串解析日期和时间。 |
getYear() | int | 获取年份。 |
getMonth() | Month | 获取月份。 |
getDayOfMonth() | int | 获取月份中的天数。 |
getDayOfWeek() | DayOfWeek | 获取星期几。 |
getDayOfYear() | int | 获取一年中的天数。 |
getHour() | int | 获取小时。 |
getMinute() | int | 获取分钟。 |
getSecond() | int | 获取秒数。 |
getNano() | int | 获取纳秒数。 |
withYear(int year) | LocalDateTime | 设置年份并返回一个新的 LocalDateTime 对象。 |
withMonth(int month) | LocalDateTime | 设置月份并返回一个新的 LocalDateTime 对象。 |
withDayOfMonth(int dayOfMonth) | LocalDateTime | 设置月份中的天数并返回一个新的 LocalDateTime 对象。 |
withDayOfYear(int dayOfYear) | LocalDateTime | 设置一年中的天数并返回一个新的 LocalDateTime 对象。 |
withHour(int hour) | LocalDateTime | 设置小时并返回一个新的 LocalDateTime 对象。 |
withMinute(int minute) | LocalDateTime | 设置分钟并返回一个新的 LocalDateTime 对象。 |
withSecond(int second) | LocalDateTime | 设置秒数并返回一个新的 LocalDateTime 对象。 |
withNano(int nanoOfSecond) | LocalDateTime | 设置纳秒数并返回一个新的 LocalDateTime 对象。 |
plusYears(long yearsToAdd) | LocalDateTime | 将指定的年数添加到日期时间并返回一个新的 LocalDateTime 对象。 |
plusMonths(long monthsToAdd) | LocalDateTime | 将指定的月数添加到日期时间并返回一个新的 LocalDateTime 对象。 |
plusWeeks(long weeksToAdd) | LocalDateTime | 将指定的周数添加到日期时间并返回一个新的 LocalDateTime 对象。 |
plusDays(long daysToAdd) | LocalDateTime | 将指定的天数添加到日期时间并返回一个新的 LocalDateTime 对象。 |
plusHours(long hoursToAdd) | LocalDateTime | 将指定的小时数添加到日期时间并返回一个新的 LocalDateTime 对象。 |
plusMinutes(long minutesToAdd) | LocalDateTime | 将指定的分钟数添加到日期时间并返回一个新的 LocalDateTime 对象。 |
plusSeconds(long secondsToAdd) | LocalDateTime | 将指定的秒数添加到日期时间并返回一个新的 LocalDateTime 对象。 |
plusNanos(long nanosToAdd) | LocalDateTime | 将指定的纳秒数添加到日期时间并返回一个新的 LocalDateTime 对象。 |
minusYears(long yearsToSubtract) | LocalDateTime | 从日期时间中减去指定的年数并返回一个新的 LocalDateTime 对象。 |
minusMonths(long monthsToSubtract) | LocalDateTime | 从日期时间中减去指定的月数并返回一个新的 LocalDateTime 对象。 |
minusWeeks(long weeksToSubtract) | LocalDateTime | 从日期时间中减去指定的周数并返回一个新的 LocalDateTime 对象。 |
minusDays(long daysToSubtract) | LocalDateTime | 从日期时间中减去指定的天数并返回一个新的 LocalDateTime 对象。 |
minusHours(long hoursToSubtract) | LocalDateTime | 从日期时间中减去指定的小时数并返回一个新的 LocalDateTime 对象。 |
minusMinutes(long minutesToSubtract) | LocalDateTime | 从日期时间中减去指定的分钟数并返回一个新的 LocalDateTime 对象。 |
minusSeconds(long secondsToSubtract) | LocalDateTime | 从日期时间中减去指定的秒数并返回一个新的 LocalDateTime 对象。 |
minusNanos(long nanosToSubtract) | LocalDateTime | 从日期时间中减去指定的纳秒数并返回一个新的 LocalDateTime 对象。 |
toLocalDate() | LocalDate | 将日期时间转换为本地日期。 |
toLocalTime() | LocalTime | 将日期时间转换为本地时间。 |
format(DateTimeFormatter formatter) | String | 将日期时间格式化为指定格式的字符串。 |
isBefore(LocalDateTime other) | boolean | 检查日期时间是否在指定日期时间之前。 |
isAfter(LocalDateTime other) | boolean | 检查日期时间是否在指定日期时间之后。 |
isEqual(LocalDateTime other) | boolean | 检查日期时间是否与指定日期时间相等。 |
compareTo(LocalDateTime other) | int | 比较日期时间与指定日期时间的顺序。 |
equals(Object obj) | boolean | 检查日期时间是否与指定对象相等。 |
hashCode() | int | 返回日期时间的哈希码。 |
toString() | String | 返回日期时间的字符串表示形式。 |