package com.cn.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import cn.hutool.json.JSONObject;
/**
* 类型描述:时间工具类
*
* @date 日期:2017年9月4日 时间:下午6:53:15
* @version 1.0
*/
public class TimeUtil {
/**
* 计算指定日期距今多少年
* 指定日期
* @return 指定日期距今多少年
*/
public static int getTime(String times) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String time = sdf.format(new Date());
String t1 = time.replace('-', '/');
String t2 = times.replace('-', '/');
@SuppressWarnings("deprecation")
Date dt1 = new Date(t1);
@SuppressWarnings("deprecation")
Date dt2 = new Date(t2);
long i = (dt1.getTime() - dt2.getTime()) / (1000 * 60 * 60 * 24);
return (int) (i / 365);
}
/**
* 返回当前时间
*
*/
public static String getNowTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(new Date());
return time;
}
/**
*
* 方法描述:日期转星期
* @version 1.0
* @param datetime
* @return
*/
public static String dateToWeek(String datetime) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 返回当前时间
*/
public static String getNowdate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String time = sdf.format(new Date());
return time;
}
/**
*/
public static String getNowdateYM(Date data) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String time = sdf.format(data);
return time;
}
public static Date addMonth(Date date, Integer num) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, num);
return calendar.getTime();
}
/**
* 方法描述:获取tkey
*
* @version 1.0
* @return
*/
public static String getTkey() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String time = sdf.format(new Date());
return time;
}
/**
* 返回当前时间
* @param format
* 时间格式化类型
* @return 指定格式类型时间
*/
public static String getNowTime(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
String time = sdf.format(new Date());
return time;
}
/**
* 方法描述:返回指定时间的指定输出格式
* @version 1.0
* @param format
* @return
*/
public static String getNowTime(Calendar calendar, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
String time = sdf.format(calendar.getTime());
return time;
}
/**
* 时间段相差秒数
*/
public static long timeDiffer(Date startDate, Date endDate) {
// milliseconds
long m_diffnum = 0;
long different = endDate.getTime() - startDate.getTime();
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
m_diffnum = 60 * 60 * 24 * elapsedDays + 60 * 60 * elapsedHours + elapsedMinutes * 60 + elapsedSeconds;
return m_diffnum;
}
/**
*
* 方法描述:查询当前日期前(后)x天的日期
* @version 1.0
* @param date
* 当前日期
* @param day
* 天数(如果day数为负数,说明是此日期前的天数)
* @return yyyy-MM-dd
* @throws ParseException
*/
public static String beforNumDay(String date, int day) {
if (ToolUtil.isEmpty(date)) {
return null;
}
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(formatter.parse(date));
c.add(Calendar.DAY_OF_YEAR, day);
return formatter.format(c.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 方法描述:计算所耗时间
*
* @version 1.0
* @param startime
* 开始时间
* @return 所耗时间
*/
public static String needTime(long startime) {
long stoptime = System.currentTimeMillis(); // 当前时间(结束时间)
int time = (int) (stoptime - startime);
return formatLoadTime(time);
}
/**
* 方法描述:格式化时间
* @version 1.0
* @param time
* @return
*/
public static String formatLoadTime(long time) {
int hours = (int) time / 3600000;
int minutes = (int) time % 3600000 / 60000;
int seconds = (int) time % 3600000 % 60000 / 1000;
int ms = (int) time % 3600000 % 60000 % 1000;
String ret = "";
if (hours > 0)
ret = hours + "时";
if (minutes > 0)
ret += minutes + "分";
if (seconds > 0)
ret += seconds + "秒";
if (ms > 0)
ret += ms + "毫秒";
return ret;
}
/**
* 方法描述:判断是否为闰年
*
* @version 1.0
* @param year
* @return
*/
public boolean isLeap(int year) {
if (((year % 100 == 0) && year % 400 == 0) || ((year % 100 != 0) && year % 4 == 0))
return true;
else
return false;
}
/**
* 方法描述:根据时间串返回正常格式时间
*
* @version 1.0
* @param date
* 例:(20170327215425)14位字符串组成的时间
* @return 例:(2017-03-27 21:54:25)
*/
public static String getDate(String date) {
if (date != null && !"".equals(date)) {
if (date.length() == 14) {
String time = date.substring(0, 4) + "-";
time += date.substring(4, 6) + "-";
time += date.substring(6, 8) + " ";
time += date.substring(8, 10) + ":";
time += date.substring(10, 12) + ":";
time += date.substring(12, 14);
return time;
} else {
return "error";
}
} else {
return "error";
}
}
/**
* 方法描述:根据yyyy-MM-dd HH:mm:ss得到yyyyMMddHHmmss
*
* @version 1.0
* @param date
* @return
*/
public static String getNumDate(String date) {
if (date == null || "".equals(date)) {
return null;
}
Pattern p = Pattern.compile("[^0-9]");
Matcher m = p.matcher(date);
return m.replaceAll("").trim();
}
/**
* 方法描述:判断指定时间是否和现在时间相隔小于输入时间
*
* @version 1.0
* @param time
* 时间
* @param l
* 多少时间以内(单位:毫秒)
* @return
*/
public static boolean dismin(String time, long l) {
if (time == null || "".equals(time)) {
return false;
}
if (l > 0) {
long a = System.currentTimeMillis();
long b = TimeUtil.getMillise(time);
if (a - b < l) {
return true;
}
} else {
return false;
}
return false;
}
/**
* 方法描述:根据yyyy-MM-dd HH:mm:ss类型时间取毫秒
* @version 1.0
* @param time
* @return
*/
public static long getMillise(String time) {
if (time == null || "".equals(time)) {
return -1;
}
Calendar calendar = Calendar.getInstance();
try {
if (time.indexOf("-") != -1 && time.indexOf(":") != -1) {
calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time));
} else {
calendar.setTime(new SimpleDateFormat("yyyyMMddHHmmss").parse(time));
}
return calendar.getTimeInMillis();
} catch (ParseException e) {
e.printStackTrace();
}
return -1;
}
/**
* 方法描述:
* @version 1.0
* @param createTime
* 发送时间
* @param reportTime
* 状态时间
* @return {0:失败,1[1~5秒]2[6~10秒]3[11~20秒]4[20秒以上]}
*/
public static String getTimeFlag(String createTime, String reportTime) {
if (createTime == null || "".equals(createTime) || reportTime == null || "".equals(reportTime)) {
return "0";
}
long startime = TimeUtil.getMillise(createTime);
long overTime = TimeUtil.getMillise(reportTime);
if (startime == -1 || overTime == -1) {
return "0";
}
if (overTime < startime) {
return "0";
}
long time = (overTime - startime) / 1000;
if (time > 20) {
// 20秒以上
return "4";
} else if (time > 10) {
// 11~20秒
return "3";
} else if (time > 5) {
// 6~10秒
return "2";
} else {
// 1~5秒
return "1";
}
}
/**
* 方法描述:获取数据库备份时间
* @version 1.0
* @return
*/
public static JSONObject getBakTime() {
return getBakTime(TimeUtil.getNowTime("yyyyMMdd"));
}
/**
* 方法描述:获取数据库备份时间
* @version 1.0
* @param time(时间格式:yyyyMMdd)
* @return
*/
public static JSONObject getBakTime(String time) {
JSONObject json = new JSONObject();
if (TimeUtil.dataFormat(time, "yyyyMMdd") != null) {
Calendar calendar = Calendar.getInstance();
try {
Date date = new SimpleDateFormat("yyyyMMdd").parse(time);
calendar.setTime(date);
if (TimeUtil.getNowTime(calendar, "dd").endsWith("4")) {
calendar.add(Calendar.DAY_OF_MONTH, -4);
String year = TimeUtil.getNowTime(calendar, "yyyy");
String month = TimeUtil.getNowTime(calendar, "MM");
String day = TimeUtil.getNowTime(calendar, "dd");
String temp_day = null;
switch (day) {
case "28":
case "29":
case "31":
temp_day = "21";
break;
default:
temp_day = CommonUtil.sub(day, "9").toString();
break;
}
if (Integer.parseInt(temp_day) < 10) {
temp_day = "0" + temp_day;
}
json.put("year", year);
json.put("month", month);
json.put("day", day);
json.put("fileName", year + month + temp_day + day);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
return json;
}
/**
* 方法描述:获取当前星期
* @version 1.0
* @param date
* @return
*/
public static String getWeekOfDate() {
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* @date 日期:2018年9月17日 时间:上午11:12:45
* @version 1.0
* @param day(距现在几天前)
* @param format(日期格式)
* @return
*/
public static String getTodayTime(int day, String format) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, day);
return TimeUtil.getNowTime(calendar, format);
}
/**
* @version 1.0
* @param date
* @param format
* @return
*/
public static String getFormatTime(String date, String format) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
Date date2 = null;
try {
date2 = simpleDateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return simpleDateFormat.format(date2);
}
/**
* 方法描述:获取时间
* @date 日期:2018年9月28日 时间:上午11:37:37
* @version 1.0
* @param date
* @return
*/
public static String getFormatTime(Object date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(date);
}
/**
* 方法描述:格式化时间
*
* @version 1.0
* @param date
* @return
*/
public static String dataFormat(String date, String format) {
String regex = null;
if (ToolUtil.isEmpty(date)) {
return null;
} else {
date = date.replace("/", "-").trim();
}
// 验证yyyyMMdd
regex = "((?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3}))(?:[0]?[1-9]|[1][012])(?:(?:[0-2]?\\d{1})|(?:[3][01]{1})))(?![\\d])";
if (date.matches(regex)) {
Date date2 = null;
try {
date2 = new SimpleDateFormat("yyyyMMdd").parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return new SimpleDateFormat(format).format(date2);
}
// 验证yyyy-MM-dd
regex = "(((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((04|08|12|16|[2468][048]|[3579][26])00))-0?2-29))";
if (date.matches(regex)) {
return date;
}
// 验证yyyy-MM-dd HH:mm:ss
regex = "(((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((04|08|12|16|[2468][048]|[3579][26])00))-0?2-29)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d";
if (date.matches(regex)) {
return getFormatTime(date, format);
}
return null;
}
}