/**
* 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();
}
}

最新文章

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

热门文章

  1. MySQL 创建删除和选择数据库
  2. Http的请求协议请求行介绍
  3. Python在for循环中更改list值的方法
  4. Selenium下Chrome配置 (含启动无痕界面)
  5. JAVA 基础编程练习题31 【程序 31 数组逆序】
  6. 动手生成 Delphi xe DBTreeview
  7. ElasticSearch——分页查询
  8. ProbCog mlnlearn的探索
  9. Spring Aop(六)——@DeclareParents介绍
  10. 《精通并发与Netty》学习笔记(13 - 解决TCP粘包拆包(一)概念及实例演示)