com.plumelog.core.util.DateUtil Maven / Gradle / Ivy
The newest version!
package com.plumelog.core.util;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
/**
* className:DateUtil
* description:DateUtil
*
* @author Frank.chen
* @version 1.0.0
*/
public class DateUtil {
// ==格式到年==
/**
* 日期格式,年份,例如:2004,2008
*/
public static final String DATE_FORMAT_YYYY = "yyyy";
// ==格式到年月 ==
/**
* 日期格式,年份和月份,例如:200707,200808
*/
public static final String DATE_FORMAT_YYYYMM = "yyyyMM";
/**
* 日期格式,年份和月份,例如:200707,2008-08
*/
public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";
// ==格式到年月日==
/**
* 日期格式,年月日,例如:050630,080808
*/
public static final String DATE_FORMAT_YYMMDD = "yyMMdd";
/**
* 日期格式,年月日,用横杠分开,例如:06-12-25,08-08-08
*/
public static final String DATE_FORMAT_YY_MM_DD = "yy-MM-dd";
/**
* 日期格式,年月日,例如:20050630,20080808
*/
public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
/**
* 日期格式,年月日,用横杠分开,例如:2006-12-25,2008-08-08
*/
public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
/**
* 日期格式,年月日,例如:2016.10.05
*/
public static final String DATE_FORMAT_POINTYYYYMMDD = "yyyy.MM.dd";
/**
* 日期格式,年月日,例如:2016年10月05日
*/
public static final String DATE_TIME_FORMAT_YYYY年MM月DD日 = "yyyy年MM月dd日";
// ==格式到年月日 时分 ==
/**
* 日期格式,年月日时分,例如:200506301210,200808081210
*/
public static final String DATE_FORMAT_YYYYMMDDHHmm = "yyyyMMddHHmm";
/**
* 日期格式,年月日时分,例如:20001230 12:00,20080808 20:08
*/
public static final String DATE_TIME_FORMAT_YYYYMMDD_HH_MI = "yyyyMMdd HH:mm";
/**
* 日期格式,年月日时分,例如:2000-12-30 12:00,2008-08-08 20:08
*/
public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI = "yyyy-MM-dd HH:mm";
// ==格式到年月日 时分秒==
/**
* 日期格式,年月日时分秒,例如:20001230120000,20080808200808
*/
public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";
/**
* 日期格式,年月日时分秒,年月日用横杠分开,时分秒用冒号分开
* 例如:2005-05-10 23:20:00,2008-08-08 20:08:08
*/
public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
// ==格式到年月日 时分秒 毫秒==
/**
* 日期格式,年月日时分秒毫秒,例如:20001230120000123,20080808200808456
*/
public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS = "yyyyMMddHHmmssSSS";
// ==特殊格式==
/**
* 日期格式,月日时分,例如:10-05 12:00
*/
public static final String DATE_FORMAT_MMDDHHMI = "MM-dd HH:mm";
private static final DateTimeFormatter DATETIME_FORMAT_NORMAL_SSS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
/* ************工具方法*************** */
/**
* @param epochMillis
* 获取yyyy-MM-dd HH:mm:ss.SSS格式的字符串
* @return yyyy-MM-dd HH:mm:ss.SSS格式的字符串
*/
public static String getDatetimeNormalStrWithMills(long epochMillis) {
return DATETIME_FORMAT_NORMAL_SSS.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), ZoneId.systemDefault()));
}
public static Integer getYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
}
public static Integer getMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH) + 1;
}
public static Integer getDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DATE);//获取日
return day;
}
public static String parseDateToStr(Date time, String timeFromat) {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.format(time);
}
public static String parseTimestampToStr(Timestamp timestamp, String timeFromat) {
SimpleDateFormat df = new SimpleDateFormat(timeFromat);
return df.format(timestamp);
}
public static String parseDateToStr(Date time, String timeFromat, final Date defaultValue) {
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.format(time);
} catch (Exception e) {
if (defaultValue != null)
return parseDateToStr(defaultValue, timeFromat);
else
return parseDateToStr(new Date(), timeFromat);
}
}
/**
* 格式化Date时间
*
* @param time Date类型时间
* @param timeFromat String类型格式
* @param defaultValue 默认时间值String类型
* @return 格式化后的字符串
*/
public static String parseDateToStr(Date time, String timeFromat, final String defaultValue) {
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.format(time);
} catch (Exception e) {
return defaultValue;
}
}
public static Date parseStrToDate(String time, String timeFromat) {
if (time == null || time.equals("")) {
return null;
}
Date date = null;
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
date = dateFormat.parse(time);
} catch (Exception e) {
}
return date;
}
public static Date parseStrToDate(String strTime, String timeFromat,
Date defaultValue) {
try {
DateFormat dateFormat = new SimpleDateFormat(timeFromat);
return dateFormat.parse(strTime);
} catch (Exception e) {
return defaultValue;
}
}
public static Date strToDate(String strTime) {
if (strTime == null || strTime.trim().length() <= 0)
return null;
Date date = null;
List list = new ArrayList(0);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
list.add(DATE_FORMAT_YYYY_MM_DD);
//list.add(DATE_FORMAT_YY_MM_DD);
list.add(DATE_FORMAT_YYYYMMDD);
list.add(DATE_FORMAT_YYYY_MM);
list.add(DATE_FORMAT_YYYYMM);
list.add(DATE_FORMAT_YYYY);
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
String format = (String) iter.next();
if (strTime.indexOf("-") > 0 && format.indexOf("-") < 0)
continue;
if (strTime.indexOf("-") < 0 && format.indexOf("-") > 0)
continue;
if (strTime.length() > format.length())
continue;
date = parseStrToDate(strTime, format);
if (date != null)
break;
}
return date;
}
public static List getMonthListOfDate(String beginDateStr, String endDateStr) {
// 指定要解析的时间格式
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM");
// 返回的月份列表
String sRet = "";
// 定义一些变量
Date beginDate = null;
Date endDate = null;
GregorianCalendar beginGC = null;
GregorianCalendar endGC = null;
List list = new ArrayList();
try {
// 将字符串parse成日期
beginDate = f.parse(beginDateStr);
endDate = f.parse(endDateStr);
// 设置日历
beginGC = new GregorianCalendar();
beginGC.setTime(beginDate);
endGC = new GregorianCalendar();
endGC.setTime(endDate);
// 直到两个时间相同
while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {
sRet = beginGC.get(Calendar.YEAR) + "-"
+ (beginGC.get(Calendar.MONTH) + 1);
list.add(sRet);
// 以月为单位,增加时间
beginGC.add(Calendar.MONTH, 1);
}
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static List getDayListOfDate(String beginDateStr, String endDateStr) {
// 指定要解析的时间格式
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
// 定义一些变量
Date beginDate = null;
Date endDate = null;
Calendar beginGC = null;
Calendar endGC = null;
List list = new ArrayList();
try {
// 将字符串parse成日期
beginDate = f.parse(beginDateStr);
endDate = f.parse(endDateStr);
// 设置日历
beginGC = Calendar.getInstance();
beginGC.setTime(beginDate);
endGC = Calendar.getInstance();
endGC.setTime(endDate);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 直到两个时间相同
while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {
list.add(sdf.format(beginGC.getTime()));
// 以日为单位,增加时间
beginGC.add(Calendar.DAY_OF_MONTH, 1);
}
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static List getYearListOfYears(int before, int behind) {
if (before < 0 || behind < 0) {
return null;
}
List list = new ArrayList();
Calendar c = null;
c = Calendar.getInstance();
c.setTime(new Date());
int currYear = Calendar.getInstance().get(Calendar.YEAR);
int startYear = currYear - before;
int endYear = currYear + behind;
for (int i = startYear; i < endYear; i++) {
list.add(Integer.valueOf(i));
}
return list;
}
public static Integer getWeekthOfYear(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(7);
c.setTime(date);
return c.get(Calendar.WEEK_OF_YEAR);
}
public static HashMap getWeekTimeOfYear(int year) {
HashMap map = new LinkedHashMap();
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
int count = getWeekthOfYear(c.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dayOfWeekStart = "";
String dayOfWeekEnd = "";
for (int i = 1; i <= count; i++) {
dayOfWeekStart = sdf.format(getFirstDayOfWeek(year, i));
dayOfWeekEnd = sdf.format(getLastDayOfWeek(year, i));
map.put(Integer.valueOf(i), "第" + i + "周(从" + dayOfWeekStart + "至" + dayOfWeekEnd + ")");
}
return map;
}
public static Integer getWeekCountOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
int count = getWeekthOfYear(c.getTime());
return count;
}
public static Date getFirstDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
return c.getTime();
}
public static Date getLastDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
return c.getTime();
}
public static Date getFirstDayOfWeek(int year, int week) {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getFirstDayOfWeek(cal.getTime());
}
public static Date getLastDayOfWeek(int year, int week) {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getLastDayOfWeek(cal.getTime());
}
public static Date getFirstDayOfMonth(int year, int month) {
month = month - 1;
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
int day = c.getActualMinimum(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
public static Date getLastDayOfMonth(int year, int month) {
month = month - 1;
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
int day = c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, day);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime();
}
public static String getDayWeekOfDate1(Date date) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
public static Integer getDayWeekOfDate2(Date date) {
Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime(date);
int weekDay = aCalendar.get(Calendar.DAY_OF_WEEK);
return weekDay;
}
public static boolean validateIsDate(String strTime) {
if (strTime == null || strTime.trim().length() <= 0)
return false;
Date date = null;
List list = new ArrayList(0);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS);
list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDD_HH_MI);
list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
list.add(DATE_FORMAT_YYYY_MM_DD);
//list.add(DATE_FORMAT_YY_MM_DD);
list.add(DATE_FORMAT_YYYYMMDD);
//list.add(DATE_FORMAT_YYYY_MM);
//list.add(DATE_FORMAT_YYYYMM);
//list.add(DATE_FORMAT_YYYY);
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
String format = (String) iter.next();
if (strTime.indexOf("-") > 0 && format.indexOf("-") < 0)
continue;
if (strTime.indexOf("-") < 0 && format.indexOf("-") > 0)
continue;
if (strTime.length() > format.length())
continue;
date = parseStrToDate(strTime.trim(), format);
if (date != null)
break;
}
if (date != null) {
System.out.println("生成的日期:" + DateUtil.parseDateToStr(date, DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS, "--null--"));
return true;
}
return false;
}
public static Date formatHhMmSsOfDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static Date addDate(Date date, int year, int month, int day, int hour, int minute, int second, int millisecond) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.YEAR, year);//加减年数
c.add(Calendar.MONTH, month);//加减月数
c.add(Calendar.DATE, day);//加减天数
c.add(Calendar.HOUR, hour);//加减小时数
c.add(Calendar.MINUTE, minute);//加减分钟数
c.add(Calendar.SECOND, second);//加减秒
c.add(Calendar.MILLISECOND, millisecond);//加减毫秒数
return c.getTime();
}
public static Long getDistanceTimestamp(Date startDate, Date endDate) {
long daysBetween = (endDate.getTime() - startDate.getTime() + 1000000) / (3600 * 24 * 1000);
return daysBetween;
}
public static Boolean compareIsSameMonth(Date date1, Date date2) {
boolean flag = false;
int year1 = getYear(date1);
int year2 = getYear(date2);
if (year1 == year2) {
int month1 = getMonth(date1);
int month2 = getMonth(date2);
if (month1 == month2) flag = true;
}
return flag;
}
public static long[] getDistanceTime(Date one, Date two) {
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
try {
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
} catch (Exception e) {
e.printStackTrace();
}
long[] times = {day, hour, min, sec};
return times;
}
public static long[] getDistanceTime(String str1, String str2) {
DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date one;
Date two;
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
try {
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
} catch (ParseException e) {
e.printStackTrace();
}
long[] times = {day, hour, min, sec};
return times;
}
public static Long getDistanceDays(String str1, String str2) throws Exception {
DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
Date one;
Date two;
long days = 0;
try {
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
days = diff / (1000 * 60 * 60 * 24);
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}
public static Date getDayBeginTime(final Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime();
}
public static Date getDayEndTime(final Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime();
}
}