java8已经出来那么长时间了,不会还有人还在用第三方日期工具类吧
使用JDK提供的LocalDateTime和Duration,了解日期的构造、计算、比较、转换等等各种姿势,足以满足日常中的各种需求,除非你的产品又提五彩斑斓的黑...
但很多同学提出:现有项目用的Date、时间戳、各种格式的日期字符串,要切换到新API成本太高了。
其实统一梳理下转换工具,也挺简单:
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;
import java.util.function.Function;
public final class DateTimes {
private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private LocalDateTime localDateTime;
private DateTimes(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
public static DateTimes now() {
return new DateTimes(LocalDateTime.now());
}
public static DateTimes of(long epochMilli) {
return new DateTimes(LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneOffset.systemDefault()));
}
public static DateTimes of(Date date) {
return new DateTimes(date.toInstant().atZone(ZoneOffset.systemDefault()).toLocalDateTime());
}
public static DateTimes of(LocalDateTime localDateTime) {
return new DateTimes(localDateTime);
}
public static DateTimes of(String text) {
return new DateTimes(LocalDateTime.parse(text, DEFAULT_FORMATTER));
}
public static DateTimes of(String text, DateTimeFormatter formatter) {
return new DateTimes(LocalDateTime.parse(text, formatter));
}
public DateTimes then(Function<LocalDateTime, LocalDateTime> then) {
this.localDateTime = (LocalDateTime)then.apply(this.localDateTime);
return this;
}
public long toMills() {
return this.localDateTime.atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli();
}
public String format() {
return this.localDateTime.format(DEFAULT_FORMATTER);
}
public String format(DateTimeFormatter formatter) {
return this.localDateTime.format(formatter);
}
public LocalDateTime toLocalDateTime() {
return this.localDateTime;
}
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
DateTimes dateTimes = (DateTimes)o;
return Objects.equals(this.localDateTime, dateTimes.localDateTime);
} else {
return false;
}
}
public int hashCode() {
return Objects.hash(new Object[]{this.localDateTime});
}
public String toString() {
return this.format();
}
}
怎么使用呢?
public static void main(String[] args) {
Date date = new Date();
System.out.println(DateTimes.of(date).equals(DateTimes.of(date.getTime())));
System.out.println(DateTimes.now().then((e) -> {
return e.plusDays(1L).plusSeconds(1L).plusHours(1L);
}).format());
LocalDateTime now = LocalDateTime.now();
LocalDateTime tomorrow = LocalDate.now().plusDays(1L).atStartOfDay();
LocalDateTime today = LocalDate.now().atStartOfDay();
System.out.println(Duration.between(now, tomorrow).getSeconds());
System.out.println(Duration.between(now, today).getSeconds());
System.out.println(Duration.between(now, tomorrow).getSeconds() - Duration.between(now, today).getSeconds() == Duration.ofDays(1L).getSeconds());
}
万里长征第一步, 前面是诗与远方,快来试试吧。