TimeUtil 工具类
2024-10-06 17:00:26
/**
* 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();
}
}
最新文章
- 【探索】无形验证码 —— PoW 算力验证
- Web缓存杂谈
- Azure Redis Cache (3) 创建和使用P级别的Redis Cache
- jzoj[1224]
- BZOJ3780 : 数字统计
- 用 Android-X86 和 VirtualBox 玩安卓游戏
- 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”
- iOS 在类实现定义中声明成员变量的怪异方式
- HDU 2079-课程时间(生成函数)
- javascript OOP 面向对象编程
- ADO.NET 扩展属性、配置文件 和 对战游戏
- static的加载先后顺序
- 将cookie 转换成字典格式
- 0x800f0845 更新1803报错
- grafana 的面板设置
- vue相关安装命令
- Django template for 循环用法
- docker安装后启动不了 解决方法
- 6,synchronized, lock 区别
- KeyPress 和KeyDown 、KeyPress之间的区别