1 简介
LocalDateTime时间模块,是Java8的新功能,推荐使用,Springboot项目可较好支持LocaDateTime与MySQL数据库时间DATETIME交互,若使用SSM,建议使用Date与MySQL时间DATETIME交互.
2 包
import java.time.LocaDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneOffset;
2 获取当前时间:now()
LocalDateTime now = LocalDateTime.now();
System.out.println("Local Date time:"+now);
System.out.println("Local date time string:" + now.toString());
- 结果
Local Date time:2020-08-23T14:01:14.319
Local date time string:2020-08-23T14:01:14.319
3 获取时间戳:秒toEpochSecond()
Long secondsTimestamp = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
System.out.println("Seconds:"+secondsTimestamp);
- 结果
Seconds:1598162474
4 时间格式转指定格式字符串:format()
4.1 LocalDateTime时间转字符串
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String currentTime = LocalDateTime.now().format(dtf);
System.out.println("Current time:"+currentTime);
- 结果
Current time:2020-08-23 14:01:14
4.2 秒转指定格式时间字符串
Long secondsCompare = 1597835368L;
LocalDateTime secondsFormat = LocalDateTime.ofEpochSecond(secondsCompare, 0, ZoneOffset.ofHours(8));
String timeFromSeconds = secondsFormat.format(dtf);
System.out.println("Time from seconds:" + timeFromSeconds);
- 结果
Time from seconds:2020-08-19 19:09:28
5 LocalDateTime格式时间字符串转为指定时间格式:parse()
LocalDateTime ldtTime = LocalDateTime.parse("2020-08-20T14:23:28.963");
System.out.println("Specific time:" + ldtTime);
String ldtStr = ldtTime.format(dtf);
System.out.println("Local date time string:" + ldtStr);
- 结果
Specific time:2020-08-20T14:23:28.963
Local date time string:2020-08-20 14:23:28
6 时间范围比较
6.1 时间转换
Long secondsCompare = 1597835368L;
LocalDateTime secondsFormat = LocalDateTime.ofEpochSecond(secondsCompare, 0, ZoneOffset.ofHours(8));
LocalDateTime now = LocalDateTime.now();
6.2 左区间判断:isBefore()
if(now.isBefore(secondsFormat)){
System.out.println("Now before given time.");
}else{
System.out.println("Now after given time.");
}
- 结果
Now after given time.
6.3 右区间判断:isAfter()
if(now.isAfter(secondsFormat)){
System.out.println("Now after given time.");
}else{
System.out.println("Now before given time.");
}
- 结果
Now after given time.
6.4 秒钟先后判断
不是多次一举,LocalDateTime的比较,需将将数据统一转换为LocaDateTime对象,和Tensorflow1.x意向
Long secondsCompare = 1597835368L;
Long secondsCompereAnother = 1598162474L;
LocalDateTime secondsFormat = LocalDateTime.ofEpochSecond(secondsCompare, 0, ZoneOffset.ofHours(8));
LocalDateTime secondsFormatAnother = LocalDateTime.ofEpochSecond(secondsCompereAnother, 0, ZoneOffset.ofHours(8));
if(secondsFormatAnother.isBefore(secondsFormat)){
System.out.println("Another time before given time.");
}else{
System.out.println("Anohter time after given time.");
}
- 结果
Anohter time after given time.
6.5 相等判断:isEqual()
LocalDateTime ldt1 = LocalDateTime.now();
LocalDateTime ldt2 = LocalDateTime.of(2020, 8, 10, 10, 10);
if(ldt1.isEqual(ldt2)){
System.out.println("ldt1 is equal to ldt2.");
}else{
System.out.println("ldt1 is not equal to ldt2");
}
- 结果
ldt1 is not equal to ldt2
7 特定格式时间字符串转LocalDateTime
String dateTemp = "2020-08-20 10:10:10";
LocalDateTime localDateTimeTemp = LocalDateTime.parse(dateTemp, dtf);
System.out.println("LocalDateTime parse time string:" + localDateTimeTemp);
String datetimeFromLocalDateTime = localDateTimeTemp.format(dtf);
System.out.println("Date transform from LocaDateTime:" + datetimeFromLocalDateTime);
- 结果
LocalDateTime parse time string:2020-08-20T10:10:10
Date transform from LocaDateTime:2020-08-20 10:10:10
8 小结
- 字符串时间使用LocalDateTime解析,需要使用parse方法现将字符串转为LocalDateTime对象;
- LocalDateTime时间对象的方法,可以解决常用的时间处理,比如isBefore(),format(),isEquals();
【参考文献】
[1]https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
[2]https://www.cnblogs.com/chuzihang/p/8276983.html
[3]https://blog.csdn.net/wangruoao/article/details/100566071
[4]https://www.cnblogs.com/qingmuchuanqi48/p/12056200.html