/**
* TODO
*
* @auther xh
* @date 6/11/19 3:32 PM
*/
public class TimeUtil {
public static final String defaultZone = "Asia/Shanghai";
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
// 获得某天最大时间 2017-10-15 23:59:59
public static Date getEndOfDay(Date date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.of(defaultZone));
LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
return Date.from(endOfDay.atZone(ZoneId.of(defaultZone)).toInstant());
}
// 获得某天最小时间 2017-10-15 00:00:00
public static Date getStartOfDay(Date date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.of(defaultZone));
LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
return Date.from(startOfDay.atZone(ZoneId.of(defaultZone)).toInstant());
}
// 获得某天最大时间 2017-10-15 23:59:59
public static Instant getEndOfDay(Instant date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(Date.from(date).getTime()), ZoneId.of(defaultZone));
LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
Date from = Date.from(endOfDay.atZone(ZoneId.of(defaultZone)).toInstant());
return from.toInstant();
}
// 获得某天最小时间 2017-10-15 00:00:00
public static Instant getStartOfDay(Instant date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(Date.from(date).getTime()), ZoneId.of(defaultZone));
LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
Date from = Date.from(startOfDay.atZone(ZoneId.of(defaultZone)).toInstant());
return from.toInstant();
}
public static String date2Str(Date date) {
if (date == null) {
return null;
}
ZoneId zone = ZoneId.of(defaultZone);
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), zone);
return dateTimeFormatter.format(localDateTime);
}
public static String instant2Str(Instant date) {
ZoneId zone = ZoneId.of(defaultZone);
LocalDateTime localDateTime = LocalDateTime.ofInstant(date, zone);
return dateTimeFormatter.format(localDateTime);
}
public static Date str2Date(String date) {
if (StringUtils.isBlank(date)) {
return null;
}
date = date.trim();
if (date.length() == 10) {
date += " 00:00:00";
}
LocalDateTime localDate = LocalDateTime.parse(date, dateTimeFormatter);
ZoneId zone = ZoneId.of(defaultZone);
Instant instant = localDate.atZone(zone).toInstant();
return Date.from(instant);
}
public static Instant str2Instant(String date) {
if (StringUtils.isBlank(date)) {
return null;
}
date = date.trim();
if (date.length() == 10) {
date += " 00:00:00";
}
LocalDateTime localDateTime = LocalDateTime.parse(date, dateTimeFormatter);
ZoneId zone = ZoneId.of(defaultZone);
return localDateTime.atZone(zone).toInstant();
}
/**
* 得到几天前的时间
*
* @param d
* @param day
* @return
*/
public static Date getDateBefore(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
return now.getTime();
}
/**
* 得到几天后的时间
*
* @param d
* @param day
* @return
*/
public static Date getDateAfter(Date d, int day) {
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
return now.getTime();
}
}