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

TimeUtil 工具类的更多相关文章

  1. 代码片段:基于 JDK 8 time包的时间工具类 TimeUtil

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “知识的工作者必须成为自己时间的首席执行官.” 前言 这次泥瓦匠带来的是一个好玩的基于 JDK ...

  2. 时间处理工具类TimeUtil

    转自:https://cnblogs.com/ityouknow/p/5662753.html 功能 Date与String之间的互相转换,以及一些特殊格式的时间字符串处理 代码 /** * 类名:T ...

  3. 超详细的Java时间工具类

    package com.td.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.Pa ...

  4. Android工具类整合

    Android-JSONUtil工具类 常用的Json工具类,包含Json转换成实体.实体转json字符串.list集合转换成json.数组转换成json public class JSONUtil ...

  5. 我的Java开发学习之旅------>工具类:将播放器的进度值转换成相应的时间格式

    在我的博客<我的Java开发学习之旅------>Java 格式化类(java.util.Formatter)基本用法,地址:http://blog.csdn.net/ouyang_pen ...

  6. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  7. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  8. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  9. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

随机推荐

  1. C之输入输出

    %d - int%ld – long (long int)%lld - long long%hd – short 短整型 (half int) %c - char%f - float%lf – dou ...

  2. 阿里RDS数据库 全量备份恢复到本地MYSQL

    阿里RDS数据库 全量备份恢复到本地MYSQL   1.首先下载RDS的全量备份 下载完成后上传到服务器备用   2.安装MySQL 5.6 首先删除机上其他的MySQL版本 检查系统是否安装其他版本 ...

  3. (翻译) How variables are allocated memory in Javascript? | scope chain | lexicial scope

    总结: 阅读下面文章需要15分钟 提问者的问题是JavaScript中内存是怎么分配的,在介绍的过程作者涉及计到了JS中 Scope Chain和调用函数call生成lexicial environm ...

  4. Ubuntu 16.04安装MySQL设置远程访问出现问题的完美解决方案(error:10061)

    一.安装mysql 安装mysql过程中,需要设置mysql的root账号的密码,不要忽略了. sudo apt-get install mysql-server apt isntall mysql- ...

  5. git clone、 remote、fetch、pull、push、remote

    git clone命令笔记 作用:远程克隆版本库 1. 克隆版本库 git clone <版本库的网址> git clone zoran@192.168.2.167:/data/gitda ...

  6. Unity动态批处理和静态批处理学习

    本文转自:http://blog.csdn.net/lyh916/article/details/45725499,请点击链接查看楼主大神原文,尊重楼主版权. 参考链接:Unity圣典:http:// ...

  7. 【AMAD】django-reversion -- 为django的model实例带来版本控制

    动机 简介 个人评分 动机 有时候数据表的数据也需要版本控制.比如论坛的帖子,作者是可以改动的,但是你想要留下每个改动的版本. 简介 django-reversion1的特点: 可以回滚一个insta ...

  8. Mac PyCharm2019激活方法

    此教程支持最新2019.2版本Pycharm及其他软件 此教程实时更新,请放心使用:如果有新版本出现猪哥都会第一时间尝试激活: pycharm官网下载地址:http://www.jetbrains.c ...

  9. 《Java语言程序设计》继承与多态

    一.动手实验:继承条件下的构造方法 调用运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent ...

  10. idea查看源码没有注释的问题

    进入idea的设置 勾选这两个 然后重新导入 页面的右上角有个下载download source的提示,点击下载即可 然后页面就要源码注释了