1.now() : 获取当前时间
//源码
public static LocalDateTime now() {
return now(Clock.systemDefaultZone());
}
//获取当前时间 年月日 时分秒
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime = " + localDateTime);
输出结果: localDateTime = 2023-03-08T21:28:17.617
2.format() : 格式化LocalDateTime
//源码
@Override // override for Javadoc and performance
public String format(DateTimeFormatter formatter) {
Objects.requireNonNull(formatter, "formatter");
return formatter.format(this);
}
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = localDateTime.format(dateTimeFormatter);
System.out.println(format);
输出结果: 2023-03-03 15:18:58
3. toLocalDate() : 日期时间转化成日期
//源码
@Override
public LocalDate toLocalDate() {
return date;
}
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();
System.out.println(localDate);
输出结果: 2023-03-08
4. isAfter() : 判断时间大小关系
// 源码
@Override // override for Javadoc and performance
public boolean isAfter(ChronoLocalDateTime<?> other) {
if (other instanceof LocalDateTime) {
return compareTo0((LocalDateTime) other) > 0;
}
return ChronoLocalDateTime.super.isAfter(other);
}
System.out.println("nowLocalDateTime = " + nowLocalDateTime);
System.out.println("oldLocalDateTime = " + oldLocalDateTime);
// 判断调用者是否在传入参数(时间)之后
boolean after = nowLocalDateTime.isAfter(oldLocalDateTime);
System.out.println(after);
输出结果:
nowLocalDateTime = 2023-03-03T22:37:32.160
oldLocalDateTime = 2023-03-03T10:33:44.160true
5. isBefore() : 判断时间大小关系
// 源码
@Override // override for Javadoc and performance
public boolean isBefore(ChronoLocalDateTime<?> other) {
if (other instanceof LocalDateTime) {
return compareTo0((LocalDateTime) other) < 0;
}
return ChronoLocalDateTime.super.isBefore(other);
}
System.out.println("nowLocalDateTime = " + nowLocalDateTime);
System.out.println("oldLocalDateTime = " + oldLocalDateTime);
// 判断调用者是否在传入参数(时间)之前
boolean before = nowLocalDateTime.isBefore(oldLocalDateTime);
System.out.println(before);
输出结果:
nowLocalDateTime = 2023-03-03T22:43:03.391
oldLocalDateTime = 2023-03-03T10:33:44.160
false
6.compareTo() : 判断时间大小关系
// 源码
@Override // override for Javadoc and performance
public int compareTo(ChronoLocalDateTime<?> other) {
if (other instanceof LocalDateTime) {
return compareTo0((LocalDateTime) other);
}
return ChronoLocalDateTime.super.compareTo(other);
}
System.out.println("nowLocalDateTime = " + nowLocalDateTime);
System.out.println("oldLocalDateTime = " + oldLocalDateTime);
// 当日期不同时,返回日期差
// 当日期相同时,返回1或-1
int nowToOld = nowLocalDateTime.compareTo(oldLocalDateTime);
int oldToNow = oldLocalDateTime.compareTo(nowLocalDateTime);
System.out.println(nowToOld);
System.out.println(oldToNow);
输出结果:
// 当两个时间日期不同时,返回日期差值
// 返回正数 即 调用方法的时间晚于传参时间 , 反之则早于传参时间
nowLocalDateTime = 2023-02-13T22:43:03.391
oldLocalDateTime = 2023-02-03T10:33:44.160
10-10
// 当两个时间日期相同时,返回1或-1
// 返回1 即 调用方法的时间晚于传参时间 , 反之则早于传参时间
nowLocalDateTime = 2023-03-03T22:43:03.391
oldLocalDateTime = 2023-03-03T10:33:44.160
1-1