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. 【Noip2015】斗地主

    题目 #include<bits/stdc++.h> using namespace std; int pai[20],T; //pai[]统计牌的数量 int n; int ans; v ...

  2. server被强制关闭,

    一个client和一个Server,两者之间建立了一个基于TCP的socket连接,在刚刚建立好连接后,尚未进行数据传输,Server端应用程序突然crush掉了,现在立刻重启Server端应用程序( ...

  3. 我对SAP Business One 项目实施的理解

    一.什么是SAP: 大家都知道ERP是什么,ERP是企业资源计划管理系统.是指建立在信息技术基础上,集信息技术与先进管理思想于一身,以系统化的管理思想,为企业员工及决策层提供决策手段的管理平台.那么问 ...

  4. 静态IP设置

    先查看自动网络的ip地址,然后设置 cmd进入DOS输入命令:ipconfig /all 设置固定IP

  5. 源码来袭:bind手写实现

    JavaScript中的this指向规则 源码来袭:call.apply手写实现与应用 理解建议:如果对this指向规则不了解的话,建议先了解this指向规则,最好还能对call和apply的使用和内 ...

  6. 第二节:重写(new)、覆写(overwrite)、和重载(overload)

    一. 重写 1. 关键字:new 2. 含义:子类继承父类中的普通方法,如果在子类中重写了一个和父类中完全相同的方法,子类中会报警告(问是否显式的隐藏父类的中的方法),如果在子类中的方法前加上new关 ...

  7. API.day01

    第1部分 JDK API 1.1 API(Application Programming Interface,应用接口程序):已经封装好可以直接调用的功能(Java中以类的形式封装) 经常使用的JDK ...

  8. (四)Java工程化--Git基础

    GIT学习参考:https://git-scm.com/book/zh/v2 常见命令 git init 初始化项目 git add *.java 添加文件到git版本控制(.java后缀的全部文件) ...

  9. windows配置Erlang环境

    1.说明 1.1 操作系统 win10x64 1.2 Erlang(['ə:læŋ])是一种通用的面向并发的编程语言,它由瑞典电信设备制造商爱立信所辖的CS-Lab开发,目的是创造一种可以应对大规模并 ...

  10. setTimeout 第三个参数秒懂

    好吧,假设你们都是从 ES6 里 promise 发现 setTimeout 还有第三个参数的,下面讲讲到底是干嘛的 setTimeout 第三个及之后的参数作用:定时器启动时候,第三个以后的参数是作 ...