TimeUtil 工具类
/**
* 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 工具类的更多相关文章
- 代码片段:基于 JDK 8 time包的时间工具类 TimeUtil
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “知识的工作者必须成为自己时间的首席执行官.” 前言 这次泥瓦匠带来的是一个好玩的基于 JDK ...
- 时间处理工具类TimeUtil
转自:https://cnblogs.com/ityouknow/p/5662753.html 功能 Date与String之间的互相转换,以及一些特殊格式的时间字符串处理 代码 /** * 类名:T ...
- 超详细的Java时间工具类
package com.td.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.Pa ...
- Android工具类整合
Android-JSONUtil工具类 常用的Json工具类,包含Json转换成实体.实体转json字符串.list集合转换成json.数组转换成json public class JSONUtil ...
- 我的Java开发学习之旅------>工具类:将播放器的进度值转换成相应的时间格式
在我的博客<我的Java开发学习之旅------>Java 格式化类(java.util.Formatter)基本用法,地址:http://blog.csdn.net/ouyang_pen ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
- [转]Java常用工具类集合
转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
随机推荐
- features its own
Gulp.js features its own built-in watch() method - no external plugin required ---- However, the Arn ...
- c++自定义数组越异常 ArrayIndexOutOfBoundsException (学习)
#include <iostream> using namespace std; const int DefaultSize = 10; class Array{public: Array ...
- [Scikit-learn] 1.1 Generalized Linear Models - Neural network models
本章涉及到的若干知识点(红字):本章节是作为通往Tensorflow的前奏! 链接:https://www.zhihu.com/question/27823925/answer/38460833 首先 ...
- 图解 HTTP 笔记(一)——了解 Web 及网络基础
本章内容:Web 建立在何种技术之上,HTTP 协议如何诞生并发展? 一.Web 基于 HTTP 通信 Web 使用一种名为 HTTP (HyperText Transfer Protocol,超文本 ...
- SpringBoot: 16.整合junit单元测试(转)
1.创建maven项目,修改pom.xml文件 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springfram ...
- 神经网络(NN)实现多分类-----Keras实现
IRIS数据集介绍 IRIS数据集(鸢尾花数据集),是一个经典的机器学习数据集,适合作为多分类问题的测试数据,它的下载地址为:http://archive.ics.uci.edu/ml/machi ...
- selenium3 web自动化测试框架 二:页面基础操作、元素定位方法封装、页面操作方法封装
学习目的: 掌握自动化框架中需要的一些基础web操作 正式步骤: 使用title_contains检查页面是否正确 # -*- coding:utf-8 -*- import time from se ...
- 【转】TCP/IP网络协议各层首部
数据包封装流程(逐层封装,逐层解封) 二层帧(二层帧中目的地址6个字节,源地址6个字节,长度/类型2个字节,二层帧共18个字节) ip头部(ip头部20字节) tcp头部(tcp头部20个字节): ...
- Linux 下清空或删除大文件内容的 5 种方法
在 Linux 终端下处理文件时,有时我们想直接清空文件的内容但又不必使用任何 Linux 命令行编辑器 去打开这些文件.那怎样才能达到这个目的呢?在这篇文章中,我们将介绍几种借助一些实用的命令来清空 ...
- tf.contrib.layers.fully_connected参数笔记
tf.contrib.layers.fully_connected 添加完全连接的图层. tf.contrib.layers.fully_connected( inputs, num_ou ...