因为Date的过时,想要使用最新的LocalDateTime,闲暇之余整理了LocalDateTime工具类,并且会持续更新。
https://www.linuxidc.com/Linux/2019-03/157267.htm
import io.swagger.annotations.Api;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Date;
/**
* @author xinyi
* @version 2019/12/8
*/
@Api(tags = "LocalDateTime工具类")
public class LocalDateTimeUtil {
//构建LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime2 = LocalDateTime.of(2019,12,8,16,12,30
);
//比较当前时间和指定时间
boolean flag = LocalDateTime.now().isBefore(localDateTime);
boolean flag2 = LocalDateTime.now().isEqual(localDateTime);
boolean flag3 = LocalDateTime.now().isAfter(localDateTime);
//获得当前时间的毫秒数
Long milli = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
//秒数
Long second = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
//将Date转换为LocalDateTime
public static LocalDateTime date2LocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
//将LocalDateTime转换为Date
public static Date localDateTime2Date(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
//将LocalDateTime转换为指定格式的字符串
public static String localDateTime2String(LocalDateTime localDateTime,String pattern) {
return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
}
//
//日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
return time.plus(number, field);
}
//日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){
return time.minus(number,field);
}
/**
* 获取两个日期的差 field参数为ChronoUnit.*
* @param startTime
* @param endTime
* @param field 单位(年月日时分秒)
* @return long
*/
public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
if (field == ChronoUnit.YEARS) return period.getYears();
if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths();
return field.between(startTime, endTime);
}
//获取一天的开始时间,2017,7,22 00:00
public static LocalDateTime getDayStart(LocalDateTime time) {
return time.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
}
//获取一天的结束时间,2017,7,22 23:59:59.999999999
public static LocalDateTime getDayEnd(LocalDateTime time) {
return time.withHour(23)
.withMinute(59)
.withSecond(59)
.withNano(999999999);
}
}
2019年12月30日更新
#LocalDateTime
1.创建对象
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime = LocalDateTime.of(2019,12,30,19,53,23);
LocalDate
LocalTime
2.和字符串的转换(Date抛出parseException,localDateTime抛出dateTimeParseException)
time-string
String str = simpleDateFormat.format(date);
string str = LocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
string-time
Date date = simpleDateFormat.parse(str);
LocalDateTime date = LocalDateTime.parse(str,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
3.和时间戳的转换
time-long
long time = date.getTime();
long time = localDateTime.atZone(ZoneId.systemDeafult()).toInstant().toEpochMilli();
long time = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
long-time
Date date = new Date(long);
LocalDateTime localDateTime = LocalDateTime.ofInstant(long,ZoneId.systemDeafult());
4.和Date互转
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDeafult());
Date date = Date.from(localDateTime.atZone(ZoneId.systemDeafult()).toInstant());
5.simpleDateFormat是线程非安全的,而DateTimeFormatter是线程安全的。
Date线程不安全,getset的时候容易出问题。
LocalDateTimes是线程安全的,类属性被final修饰。