public class DateTimeUtil {
static SimpleDateFormat format;
private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();
/**
* 日期格式:yyyy-MM-dd HH:mm:ss e.g. 2019-05-11 12:09:14
**/
public static final String DF_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
/**
* 日期格式:yyyy-MM-dd HH:mm e.g. 2019-05-11 12:09
**/
public static final String DF_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
/**
* 日期格式:yyyy-MM-dd e.g. 2019-05-11
**/
public static final String DF_YYYY_MM_DD = "yyyy-MM-dd";
/**
* 日期格式:HH:mm:ss e.g. 12:09:14
**/
public static final String DF_HH_MM_SS = "HH:mm:ss";
/**
* 日期格式:HH:mm e.g. 09:14
**/
public static final String DF_HH_MM = "HH:mm";
private final static long minute = 60 * 1000;// 1分钟
private final static long hour = 60 * minute;// 1小时
private final static long day = 24 * hour;// 1天
private final static long month = 31 * day;// 月
private final static long year = 12 * month;// 年
public DateTimeUtil() {
}
/**
* 将日期格式化成友好的字符串:几分钟前、几小时前、几天前、几月前、几年前、刚刚
*
* @param date
* @return
*/
public static String formatFriendly(Date date) {
if (date == null) {
return null;
}
long diff = new Date().getTime() - date.getTime();
long r = 0;
if (diff > year) {
r = (diff / year);
return r + "年前";
}
if (diff > month) {
r = (diff / month);
return r + "个月前";
}
if (diff > day) {
r = (diff / day);
return r + "天前";
}
if (diff > hour) {
r = (diff / hour);
return r + "个小时前";
}
if (diff > minute) {
r = (diff / minute);
return r + "分钟前";
}
return "刚刚";
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化 e.g. 2019-05-11 12:09:14
*
* @param dateL 日期
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDateTime(long dateL) {
SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
Date date = new Date(dateL);
return sdf.format(date);
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param dateL 日期 long形
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDateTime(long dateL, String formater) {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(new Date(dateL));
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param date 日期
* @param formater
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDateTime(Date date, String formater) {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(date);
}
/**
* 将日期字符串转成日期 e.g. 2019-05-11 12:09:14
*
* @param strDate 字符串日期
* @return java.util.date日期类型
*/
@SuppressLint("SimpleDateFormat")
public static Date parseDate(String strDate) {
DateFormat dateFormat = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
Date returnDate = null;
try {
returnDate = dateFormat.parse(strDate);
} catch (ParseException e) {
}
return returnDate;
}
/**
* 获取系统当前日期
*
* @return
*/
public static Date gainCurrentDate() {
return new Date();
}
/**
* 验证日期是否比当前日期早
*
* @param target1 比较时间1
* @param target2 比较时间2
* @return true 则代表target1比target2晚或等于target2,否则比target2早
*/
public static boolean compareDate(Date target1, Date target2) {
boolean flag = false;
try {
String target1DateTime = formatDateTime(target1, DF_YYYY_MM_DD_HH_MM_SS);
String target2DateTime = formatDateTime(target2, DF_YYYY_MM_DD_HH_MM_SS);
if (target1DateTime.compareTo(target2DateTime) <= 0) {
flag = true;
}
} catch (Exception e) {
System.out.println("比较失败,原因:" + e.getMessage());
}
return flag;
}
/**
* 对日期进行增加操作
*
* @param target 需要进行运算的日期
* @param hour 小时
* @return
*/
public static Date addDateTime(Date target, double hour) {
if (null == target || hour < 0) {
return target;
}
return new Date(target.getTime() + (long) (hour * 60 * 60 * 1000));
}
/**
* 对日期进行相减操作
*
* @param target 需要进行运算的日期
* @param hour 小时
* @return
*/
public static Date subDateTime(Date target, double hour) {
if (null == target || hour < 0) {
return target;
}
return new Date(target.getTime() - (long) (hour * 60 * 60 * 1000));
}
/**
* 获取系统时间的方法:月/日 时:分:秒
*/
public static String getFormateDate() {
Calendar calendar = Calendar.getInstance();
int month = (calendar.get(Calendar.MONTH) + 1);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String systemTime = (month < 10 ? "0" + month : month) + "/" + (day < 10 ? "0" + day : day) + " " + (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second);
return systemTime;
}
/**
* 获取系统时间的方法:时:分:秒
*/
public static String getHourAndMinute() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
return (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute);
}
/**
* 获取系统时间的方法:时
*/
public static String getHour() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
return ((hour < 10 ? "0" + hour : hour) + "");
}
/**
* 将2017-07-10 00:00:00 换2017-07-10
*
* @param strDate
* @return
*/
public static String strFormatStr(String strDate) {
if (strDate.equals("")) {
return "";
}
return dateToStr(strToDate(strDate));
}
/**
* 2015-01-07 15:05:34
*
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date strToDateHHMMSS(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 2015-01-07
*
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 2015.01.07
*
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date strToDateDorp(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
@SuppressLint("SimpleDateFormat")
public static String dateToStr(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 传入一个String转化为long
*/
@SuppressLint("SimpleDateFormat")
public static Long stringParserLong(String param) throws ParseException {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(param).getTime();
}
/**
* 当前时间转换为long
*/
@SuppressLint("SimpleDateFormat")
public static Long currentDateParserLong() throws ParseException {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(format.format(Calendar.getInstance().getTime())).getTime();
}
/**
* 当前时间 如: 2013-04-22 10:37:00
*/
@SuppressLint("SimpleDateFormat")
public static String getCurrentDate() {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return format.format(Calendar.getInstance().getTime());
}
/**
* 当前时间 如: 10:37
*/
@SuppressLint("SimpleDateFormat")
public static String getCurrentDateHHMM() {
format = new SimpleDateFormat("HH:mm");
return format.format(Calendar.getInstance().getTime());
}
/**
* 当前时间 如: 10:37
*
* @throws ParseException
*/
@SuppressLint("SimpleDateFormat")
public static String getCurrentDateHHMMSS() {
format = new SimpleDateFormat("HH:mm:ss");
return format.format(Calendar.getInstance().getTime());
}
/**
* 当前时间 如: 20130422
*/
@SuppressLint("SimpleDateFormat")
public static String getCurrentDateString() {
format = new SimpleDateFormat("yyyyMMddHHmm");
return format.format(Calendar.getInstance().getTime());
}
/**
* 当前时间 如: 2013-04-22
*/
@SuppressLint("SimpleDateFormat")
public static String getCurrentTime() {
format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(Calendar.getInstance().getTime());
}
@SuppressLint("SimpleDateFormat")
public static String getSWAHDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(Calendar.getInstance().getTime());
}
@SuppressLint("SimpleDateFormat")
public static Long stringToLongD(String param) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.parse(param.substring(0, param.length() - 4)).getTime();
}
@SuppressLint("SimpleDateFormat")
public static Long stringToLong(String param) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
return format.parse(param).getTime();
}
/**
* 获取两个日期之间的间隔天数
*
* @return
*/
@SuppressLint("SimpleDateFormat")
public static int getGapCount(Date startDate, Date endDate) {
Calendar fromCalendar = Calendar.getInstance();
fromCalendar.setTime(startDate);
fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
fromCalendar.set(Calendar.MINUTE, 0);
fromCalendar.set(Calendar.SECOND, 0);
fromCalendar.set(Calendar.MILLISECOND, 0);
Calendar toCalendar = Calendar.getInstance();
toCalendar.setTime(endDate);
toCalendar.set(Calendar.HOUR_OF_DAY, 0);
toCalendar.set(Calendar.MINUTE, 0);
toCalendar.set(Calendar.SECOND, 0);
toCalendar.set(Calendar.MILLISECOND, 0);
return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
}
/**
* 日期转换成Java字符串
*
* @param date
* @return str
*/
@SuppressLint("SimpleDateFormat")
public static String DateToStr(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = format.format(date);
return str;
}
/**
* 字符串转换成日期
*
* @param str
* @return date
*/
@SuppressLint("SimpleDateFormat")
public static Date StrToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 字符串转换成日期
*
* @param str
* @return date
*/
@SuppressLint("SimpleDateFormat")
public static Date StrToDateDrop(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* @param time
* @return
*/
@SuppressLint("SimpleDateFormat")
public static long getLongTime(String time) {
long ct = 0;
try {
format = new SimpleDateFormat("HH:mm:ss");
ct = format.parse(time).getTime();
} catch (Exception e) {
e.printStackTrace();
}
return ct;
}
/**
* 判断两日期是否同一天
*
* @param str1
* @param str2
* @return
*/
@SuppressLint("SimpleDateFormat")
public static boolean isSameDay(String str1, String str2) {
Date day1 = null, day2 = null;
day1 = DateTimeUtil.strToDate(str1);
day2 = DateTimeUtil.strToDate(str2);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String ds1 = sdf.format(day1);
String ds2 = sdf.format(day2);
if (ds1.equals(ds2)) {
return true;
} else {
return false;
}
}
/**
* 获取两个日期的时间差
*/
@SuppressLint("SimpleDateFormat")
public static int getTimeInterval(String date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int interval = 0;
try {
Date currentTime = new Date();// 获取现在的时间
Date beginTime = dateFormat.parse(date);
interval = (int) ((beginTime.getTime() - currentTime.getTime()) / (1000));// 时间差
// 单位秒
} catch (ParseException e) {
e.printStackTrace();
}
return interval;
}
/**
* 获取两个日期的时间差 yyyy.MM.dd HH.mm.ss
*/
@SuppressLint("SimpleDateFormat")
public static int getInterval(String bDate, String eDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
int interval = 0;
try {
Date currentTime = dateFormat.parse(eDate);// 获取现在的时间
Date beginTime = dateFormat.parse(bDate);
interval = (int) ((beginTime.getTime() - currentTime.getTime()));// 时间差
// 单位秒
} catch (ParseException e) {
e.printStackTrace();
}
return interval;
}
/**
* 两个时间之差 求出一个long Time
*
* @param date
* @return
*/
@SuppressLint("SimpleDateFormat")
public static long getTime(String date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long diff = 0;
try {
Date currentTime = new Date();// 获取现在的时间
Date getdate = df.parse(date);
diff = getdate.getTime() - currentTime.getTime();
} catch (Exception e) {
}
return diff;
}
/**
* 日期转换成Java字符串
*
* @param DATE1
* @param DATE2
* @return
*/
@SuppressLint("SimpleDateFormat")
public static int compare_date(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() >= dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
/**
* 传入时间 算出星期几
*
* @param str 2014年1月3日
* @param days 1:2014年1月4日 类推
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDate(String str, int days) {
String dateStr = "";
try {
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
Date date = df.parse(str);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date d = dateFormat.parse(dateFormat.format(date));
c.setTime(d);
c.add(Calendar.DAY_OF_MONTH, days);
switch (c.get(Calendar.DAY_OF_WEEK) - 1) {
case 0:
dateStr = "周日";
break;
case 1:
dateStr = "周一";
break;
case 2:
dateStr = "周二";
break;
case 3:
dateStr = "周三";
break;
case 4:
dateStr = "周四";
break;
case 5:
dateStr = "周五";
break;
case 6:
dateStr = "周六";
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return dateStr;
}
/**
* 产生周序列,即得到当前时间所在的年度是第几周
*
* @return
*/
public static String getSeqWeek() {
Calendar c = Calendar.getInstance(Locale.CANADA);
String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
if (week.length() == 1)
week = "0" + week;
String year = Integer.toString(c.get(Calendar.YEAR));
return year + week;
}
/**
* 获取一个月的最后一天
*
* @param dat
* @return
*/
public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
String str = dat.substring(0, 8);
String month = dat.substring(5, 7);
int mon = Integer.parseInt(month);
if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
str += "31";
} else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
str += "30";
} else {
if (isLeapYear(dat)) {
str += "29";
} else {
str += "28";
}
}
return str;
}
/**
* 判断是否润年
*
* @param ddate
* @return
*/
public static boolean isLeapYear(String ddate) {
/**
* 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
* 3.能被4整除同时能被100整除则不是闰年
*/
Date d = strToDate(ddate);
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(d);
int year = gc.get(Calendar.YEAR);
if ((year % 400) == 0)
return true;
else if ((year % 4) == 0) {
if ((year % 100) == 0)
return false;
else
return true;
} else
return false;
}
/**
* 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
*/
public static String getNextDay(String nowdate, String delay) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String mdate = "";
Date d = strToDate(nowdate);
long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;
d.setTime(myTime * 1000);
mdate = format.format(d);
return mdate;
} catch (Exception e) {
return "";
}
}
/**
* 格式化时间
*
* @param time
* @return - 1:昨天;-2:前天;0:今天; -8:默认时间;-7:传入的参数不对
*/
private int formatDateTime(String time) {
SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm");
if (time == null || "".equals(time)) {
return -7;
}
Date date = null;
try {
date = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar current = Calendar.getInstance();
Calendar today = Calendar.getInstance(); // 今天
today.set(Calendar.YEAR, current.get(Calendar.YEAR));
today.set(Calendar.MONTH, current.get(Calendar.MONTH));
today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
// Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
Calendar yesterday = Calendar.getInstance(); // 昨天
yesterday.set(Calendar.YEAR, current.get(Calendar.YEAR));
yesterday.set(Calendar.MONTH, current.get(Calendar.MONTH));
yesterday.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH) - 1);
yesterday.set(Calendar.HOUR_OF_DAY, 0);
yesterday.set(Calendar.MINUTE, 0);
yesterday.set(Calendar.SECOND, 0);
Calendar yesterday2 = Calendar.getInstance(); // 前天
yesterday2.set(Calendar.YEAR, current.get(Calendar.YEAR));
yesterday2.set(Calendar.MONTH, current.get(Calendar.MONTH));
yesterday2.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH) - 2);
yesterday2.set(Calendar.HOUR_OF_DAY, 0);
yesterday2.set(Calendar.MINUTE, 0);
yesterday2.set(Calendar.SECOND, 0);
current.setTime(date);
if (current.after(today)) {
return 0;
} else if (current.before(today) && current.after(yesterday)) {
return -1;
} else if (current.before(yesterday) && current.after(yesterday2)) {
return -2;
} else {
return -8;
}
}
/**
* 格式化时间
*
* @param date long形
* @return 格式化的字符串,今天,昨天,实际日期
*/
public static String getTodayOrYesterday(long date) {//date 是存储的时间戳
//所在时区时8,系统初始时间是1970-01-01 80:00:00,注意是从八点开始,计算的时候要加回去
int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
long today = (System.currentTimeMillis() + offSet) / 86400000;
long start = (date + offSet) / 86400000;
long intervalTime = start - today;
//-2:前天,-1:昨天,0:今天,1:明天,2:后天
String strDes = "";
if (intervalTime == 0) {
strDes = "今天";
} else if (intervalTime == -1) {
strDes = "昨天";
} else {
strDes = formatDateTime(date);//直接显示时间
}
return strDes;
}
}