package com.inspur.jobSchedule.util;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger; import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar; /**
* 日期处理通用类
*
* @author
* @version 1.0
*/
public final class DateUtil extends DateUtils { private static Logger logger = Logger.getLogger("DateUtil"); /**
* 返回date1 - date2 的分钟数
*
* @param date1 日期1
* @param date2 日期2
* @return 相隔分钟数
*/
public static long getMinites(String date1, String date2) {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); long millSec = 0L;
long diffMins = 0L;
try {
millSec = dfs.parse(date1).getTime() - dfs.parse(date2).getTime();
millSec = Math.abs(millSec);
diffMins = (millSec / 1000) / 60;
} catch (ParseException e) {
logger.error("getMinites exception:" + e.getMessage());
}
return diffMins;
} /**
* 返回date1 - date2 的分钟数
*
* @param date1 日期1
* @param date2 日期2
* @return 相隔分钟数
*/
public static long getMins(String date1, String date2) {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long millSec = 0L;
long diffMins = 0L;
try {
millSec = dfs.parse(date1).getTime() - dfs.parse(date2).getTime();
millSec = Math.abs(millSec);
diffMins = (millSec / 1000) / 60;
} catch (ParseException e) {
logger.error("getMins exception:" + e);
}
return diffMins;
} /**
* 返回date1 - date2 的秒数
*
* @param date1 日期1
* @param date2 日期2
* @return 相隔秒数
*/
public static long getDiffSeconds(String date1, String date2) {
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long millSec = 0L;
long diffSeconds = 0L;
try {
millSec = dfs.parse(date1).getTime() - dfs.parse(date2).getTime();
millSec = Math.abs(millSec);
diffSeconds = millSec / 1000;
} catch (ParseException e) {
// e.printStackTrace();
logger.error("getDiffSeconds exception:" + e);
}
return diffSeconds;
} /**
* 格式化日期 : yyyy-MM-dd HH:mm:ss
*
* @param date 日期
* @return 日期字符串,如果date为空返回空串("")
*/
public static String getFormatDateString(Date date) {
if (date == null) {
return "";
} return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
} /**
* 任务调度 格式化日期 : yyyy-MM-dd HH:mm:ss.SSS
*
* @param date
* @return 日期字符串,如果date为空返回空串("")
* @since 1.0
*/
public static String getFormatDateForSchedule(Date date) {
if (date == null) {
return "";
} return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date);
} /**
* 格式化日期 : yyyy-MM-dd HH:mm
*
* @param date 日期
* @return 日期字符串,如果date为空返回空串("")
*/
public static String getFormatDateTimeString(Date date) {
if (date == null) {
return "";
}
return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
} /**
* 格式化日期 : yyyy-MM-dd
*
* @param date 日期
* @return 日期字符串,如果为空返回空串("")
*/
public static String getDateString(Date date) {
if (date == null) {
return "";
} return new SimpleDateFormat("yyyy-MM-dd").format(date);
} /**
* 格式化时间 : HH:mm:ss
*
* @param date 日期对象
* @return 格式化日期字符串
*/
public static String getTimeString(Date date) {
if (date == null) {
return "";
} return new SimpleDateFormat("HH:mm:ss").format(date);
} /**
* 由字符串转换为日期类型
*
* @param str 日期字符串
* @param format 格式化字符串
* @return 日期对象
*/
public static Date getDate(String str, String format) {
try {
return new SimpleDateFormat(format).parse(str);
} catch (ParseException e) {
return null;
} catch (RuntimeException e) {
return null;
}
} /**
* 获取日期对应的时间,其中年月日为当前的年月日,时间为参数中的时间
*
* @param time 时间
* @return 日期
*/
public static Date getDateFromTime(Time time) {
Calendar c = Calendar.getInstance();
try {
c.setTime(new SimpleDateFormat("HH:mm:ss").parse(time.toString()));
} catch (ParseException e) {
logger.error("getDateFromTime exception:" + e.getMessage());
return null;
} catch (RuntimeException e) {
logger.error("getDateFromTime exception:" + e);
return null;
}
Calendar cal = Calendar.getInstance();
c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DATE));
return c.getTime();
} /**
* 返回毫秒级别数据
*
* @param date 日期对象
* @return mmssSSS 格式时间
*/
public static String getmmssSSS(Date date) {
return new SimpleDateFormat("mmssSSS").format(date);
} /**
* 日期格式化(业务包使用,勿删)
*
* @param sDate 原始串
* @param sFmt 格式
* @return 输出
*/
public static Date toDate(String sDate, String sFmt) {
Date dt = null;
try {
dt = new SimpleDateFormat(sFmt).parse(sDate);
} catch (ParseException e) {
return dt;
}
return dt;
} /**
* 将日期格式化为长格式(业务包使用,勿删)
*
* @param dateDate 原始日期
* @return 输出
*/
public static String dateToStrLong(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(dateDate);
return dateString;
} /**
* 将日期格式化为长格式到毫秒(业务包使用,勿删)
*
* @param dateDate 原始日期
* @return 输出
*/
public static String dateToStrLongSSS(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ssSSS");
String dateString = formatter.format(dateDate);
return dateString;
} /**
* FormatDateTime(业务包使用,勿删)
*
* @param strDateTime 原始日期
* @param strFormat 转换格式
* @return 转换结果
*/
public static String formatDateTime(String strDateTime, String strFormat) {
String sDateTime = strDateTime;
Calendar cal = parseDateTime(strDateTime);
SimpleDateFormat sdf = null;
sdf = new SimpleDateFormat(strFormat);
sDateTime = sdf.format(cal.getTime());
return sDateTime;
} /**
* 将日期解析为calendar对象(业务包使用,勿删)
*
* @param baseDate 原始日期
* @return calendar对象
*/
public static Calendar parseDateTime(String baseDate) {
Calendar cal = null;
cal = new GregorianCalendar();
int yy = Integer.parseInt(baseDate.substring(0, 4));
int mm = Integer.parseInt(baseDate.substring(5, 7)) - 1;
int dd = Integer.parseInt(baseDate.substring(8, 10));
int hh = 0;
int mi = 0;
int ss = 0;
if (baseDate.length() > 12) {
hh = Integer.parseInt(baseDate.substring(11, 13));
mi = Integer.parseInt(baseDate.substring(14, 16));
ss = Integer.parseInt(baseDate.substring(17, 19));
}
cal.set(yy, mm, dd, hh, mi, ss);
return cal;
} /**
* 获取指定格式的当前时间(业务包使用,勿删)
*
* @param sformat 日期格式
* @return 格式化日期串
*/
public static String getUserDate(String sformat) {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(sformat);
String dateString = formatter.format(currentTime);
return dateString;
} /**
* @param mss 要转换的毫秒数 单位为英文单位 days,hour,分钟,秒
* @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
*/
public static String formatDuring(long mss) {
long days = mss / (1000 * 60 * 60 * 24);
long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
long seconds = (mss % (1000 * 60)) / 1000;
return days + " days " + hours + " hours " + minutes + " minutes "
+ seconds + " seconds ";
} /**
* @param mss 要转换的毫秒数 单位为汉字 天,小时,分钟,秒
* @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
*/
public static String formatDuringCN(long mss) {
long days = mss / (1000 * 60 * 60 * 24);
long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
long seconds = (mss % (1000 * 60)) / 1000;
return days + " 天 " + hours + " 小时 " + minutes + " 分" + seconds + " 秒 ";
} /**
* 运行时间计算 任务调度专用
*
* @param mss
* @return
* @since 1.0
*/
public static String formatDurationCN(long mss) {
long days = mss / (1000 * 60 * 60 * 24);
long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
long seconds = (mss % (1000 * 60)) / 1000;
long msec = mss % 1000;
String result = msec + "毫秒";
if (seconds != 0L) {
result = seconds + "秒" + result;
}
if (minutes != 0L) {
result = minutes + "分" + result;
}
if (hours != 0L) {
result = hours + "小时" + result;
}
if (days != 0L) {
result = days + "天" + result;
}
return result;
} /**
* @param begin 时间段的开始
* @param end 时间段的结束
* @return 输入的两个Date类型数据之间的时间间格用* days * hours * minutes * seconds的格式展示
*/
public static String formatDuring(Date begin, Date end) {
return formatDuring(end.getTime() - begin.getTime());
} /**
* @param begin 时间段的开始
* @param end 时间段的结束
* @return 输入的两个Date类型数据之间的时间间格用* days * hours * minutes * seconds的格式展示
*/
public static String formatDuringCN(Date begin, Date end) {
return formatDuringCN(end.getTime() - begin.getTime());
} /**
* 任务调度返回任务运行时间
*
* @param begin 时间段的开始
* @param end 时间段的结束
* @return
* @since 1.0
*/
public static String formatDurationCN(Date begin, Date end) {
return formatDurationCN(end.getTime() - begin.getTime());
} /**
* 格式化日期 : yyyy-MM-dd HH:mm:ss
*
* @param date 日期
* @return 返回年 yyyy
*/
public static String getFormatDateYear(Date date) {
if (date == null) {
return "";
} return new SimpleDateFormat("yyyy").format(date);
} /**
* 格式化日期 : yyyy-MM-dd HH:mm:ss
*
* @param date 日期
* @return 返回年 yyyy
*/
public static String getFormatDateMonth(Date date) {
if (date == null) {
return "";
} return new SimpleDateFormat("yyyy-MM").format(date);
} /**
* 返回上一个月的日期 yyyy-MM
*
* @param date 日期
* @return 返回年 yyyy
*/
public static String getFormatDateLastMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1);
Date newDate = calendar.getTime();
return new SimpleDateFormat("yyyy-MM").format(newDate);
} /**
* 返回下一个月的日期 yyyy-MM
*
* @param date 日期
* @return 返回年 yyyy
*/
public static String getFormatDateNextMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
Date newDate = calendar.getTime();
return new SimpleDateFormat("yyyy-MM").format(newDate);
} public static String getLastNDays(Date date, int days) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, -days);
Date newDate = calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDate);
} public static String getLastNHours(Date date, int hours) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, -hours);
Date newDate = calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDate);
} public static String getLastNMins(Date date, int minutes) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, -minutes);
Date newDate = calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDate);
} public static String getLastNSecs(Date date, int seconds) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.SECOND, -seconds);
Date newDate = calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDate);
} public static String getLastNMinsWithParse(Date date, int minutes,
String parse) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, -minutes);
Date newDate = calendar.getTime();
return new SimpleDateFormat(parse).format(newDate);
} /**
* 获取最近的正好5分钟的时间
*
* @return
*/
public static Date getLastJust5Min() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)
- calendar.get(Calendar.MINUTE) % 5);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date newDate = calendar.getTime();
return newDate;
} /**
* 获取最近的正好5分钟的时间字符串
*
* @return
*/
public static String getLastJust5MinStr() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)
- calendar.get(Calendar.MINUTE) % 5);
Date newDate = calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(newDate);
} /**
* 获取最近的正好15分钟的时间字符串
*
* @return
*/
public static String getLastJust15MinStr() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)
- calendar.get(Calendar.MINUTE) % 15);
Date newDate = calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(newDate);
} /**
* 格式化日期 : yyyy-MM-dd HH:mm:ss
*
* @param time 这里为毫秒级的时间类型
* @return 返回格式化的日期
* @since 1.0
*/
public static String getFormatDateString(long time) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date(time));
} /**
* 比较两个日期是否相等
*
* @param sourceDate
* @param targetDate
* @return 如果相等返回true 不相等返回false
* @since 1.0
*/
public static boolean compareDate(String sourceDate, String targetDate) {
boolean flag = false;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dt1 = df.parse(sourceDate);
Date dt2 = df.parse(targetDate);
if (dt1.getTime() > dt2.getTime()) {
} else if (dt1.getTime() < dt2.getTime()) {
} else {
flag = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return flag;
} /**
* 比较两个日期相等
*
* @param sourceDate
* @param targetDate
* @return
* @since 1.0
*/
public static boolean compareDate(Date sourceDate, Date targetDate) {
boolean flag = false;
if (null == sourceDate && null == targetDate) {
flag = true;
}
if (null != sourceDate && null != targetDate) {
try { if (sourceDate.getTime() > targetDate.getTime()) {
} else if (sourceDate.getTime() < targetDate.getTime()) {
} else {
flag = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
return flag;
} /**
* 当前时间前移30分钟
*
* @param endtime
* @return
* @Time 2018年8月21日
* @author guoqiaozhi
*/
public static String getStarttimeHhour(String endtime) {
Date startDate = DateUtil.addMinutes(DateUtil.getDate(endtime, "yyyy-MM-dd HH:mm:ss"), -30);
return DateUtil.getFormatDateString(startDate);
} /**
* 当前时间前移一小时
*
* @param endtime
* @return
* @Time 2018年8月21日
* @author guoqiaozhi
*/
public static String getStarttimeHour(String endtime) {
Date startDate = DateUtil.addHours(DateUtil.getDate(endtime, "yyyy-MM-dd HH:mm:ss"), -1);
return DateUtil.getFormatDateString(startDate);
} /**
* 当前时间前移一天
*
* @param endtime
* @return
* @Time 2018年8月21日
* @author guoqiaozhi
*/
public static String getStarttimeDay(String endtime) {
Date startDate = DateUtil.addDays(DateUtil.getDate(endtime, "yyyy-MM-dd HH:mm:ss"), -1);
return DateUtil.getFormatDateString(startDate);
}
}

JAVA时间工具类,在维护的项目里的的更多相关文章

  1. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  2. 小记Java时间工具类

    小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...

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

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

  4. java时间工具类

    在项目中,很多地方需要根据时间获取相应的数据,将时间格式化,或者时间比较等相关操作.一个良好的工具类不仅可以减少代码冗余,还能促进业务处理,加快进度. /** * @author: lxw * @Da ...

  5. 一个好的Java时间工具类DateTime

    此类的灵感来源于C# 虽然网上有什么date4j,但是jar太纠结了,先给出源码,可以继承到自己的util包中,作为一个资深程序员,我相信都有不少好的util工具类,我也希望经过此次分享,能带动技术大 ...

  6. Java 时间工具类

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  1.Calendar 转化 String  ...

  7. java时间工具类,时间相互转换

    /* * @author XueWeiWei * @date 2019/8/26 16:22 */ package com.nps.utils; import java.text.ParseExcep ...

  8. JAVA时间工具类用法

    1.获得N天前的TIMESTAMP Calendar cl = Calendar.getInstance(); cl.add(Calendar.DAY_OF_YEAR, -7); Date date ...

  9. java日期工具类DateUtil-续二

    该版本是一次较大的升级,农历相比公历复杂太多(真佩服古人的智慧),虽然有规律,但涉及到的取舍.近似的感念太多,况且本身的概念就已经很多了,我在网上也是查阅了很多的资料,虽然找到一些计算的方法,但都有些 ...

随机推荐

  1. JDK动态代理(Proxy)的两种实现方式

    JDK自带的Proxy动态代理两种实现方式 前提条件:JDK Proxy必须实现对象接口 so,创建一个接口文件,一个实现接口对象,一个动态代理文件 接口文件:TargetInterface.java ...

  2. 越光后端开发——ygapi(1.新建项目ygapi、新建MySQL数据库yg、项目连接数据库)

    1.新建MySQL数据库 show databases;//查看已经有的数据库 create database yg; 2.新建项目ygapi 1.使用pycharm新建django项目取名ygapi ...

  3. Java9 接口细谈

    java9对接口进行了改进,允许在接口中定义默认方法和类方法并且都支持方法的实现.同时添加了一种私有方法,私有方法也可提供方法实现. 注:下面语法只有在Java8以上的版本才允许在接口定义默认方法.类 ...

  4. CMDB服务器管理系统【s5day90】:API构造可插拔式插件逻辑

    1.服务器端目录结构: 1.__init__.py from django.conf import settings from repository import models import impo ...

  5. react实战项目开发(2) react几个重要概念以及JSX语法

    前言 前面我们已经学习了利用官方脚手架搭建一套可以应用在生产环境下的React开发环境.那么今天这篇文章主要先了解几个react重要的概念,以及讲解本文的重要知识JSX语法 React重要概念 [思想 ...

  6. HTTP中application/x-www-form-urlencoded字符说明

    一.概述 在学习ajax的时候,如果用post请求,需要设置如下代码. ajax.setRequestHeader("content-type","application ...

  7. MYSQL实战

    基础架构 更新操作 日志模块 redo log 和 binlog 两阶段提交: prepare commit 事务隔离 读未提交:别人改数据的事务尚未提交,我在我的事务中也能读到.读已提交:别人改数据 ...

  8. XML与HTML的作用不同

    1. html是用来显示数据的:xml是用来描述数据.存放数据的,所以可以作为持久化的介质!Html将数据和显示结合在一起,在页面中把这数据显示出来:xml 则将数据和显示分开. XML被设计用来描述 ...

  9. IDEAL字体颜色修改

    IDEA 炫酷的主题字体颜色设置(基于IDEA 2018)前言: IDEA中主题可以更换,大家可以直接到 http://www.riaway.com/  网站或 http://color-themes ...

  10. mysql慢日志, 锁表情况查询

    2018-5-29 9:10:15 星期二 锁表情况查询: show OPEN TABLES where In_use > ; 慢日志记录: 1. 修改配置文件, 重启服务后永久生效 slow_ ...