public class DateUtil {
public static final String LONG_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String LONG_DATE_FORMAT3 = "yyyy-MM-dd HH:mm";
public static final String LONG_DATE_FORMAT1 = "yyyyMMddHHmmss";
public static final String LONG_DATE_FORMAT2 = "yyyyMMddHHmm";
public static final String LONG_DATE_FORMAT0 = "yyyy-MM-dd HH:mm:ss.SSS";
public static final String SHORT_DATE_FORMAT = "yyyy-MM-dd";
public static final String SHORT_TIME_FORMAT = "HH:mm:ss";
public static final String SHORT_MONTH_FORMAT = "yyyy-MM";
public static final String SHORT_YEAR_FORMAT = "yyyy";
public static final String YEAR_MONTH = "yyyyMM";
public static final String MONTH_FORMAT = "MM";
public static final String DATE_FORMAT = "dd";
public static DateFormat dateTimeMinFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
/**
* 字符串转日期
*/
public static Date stringToDate(String fromat, String dateStr) {
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat(fromat);
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/**
* 取得n月前时间
*/
public static Date getTimeByMonth(Date date, int month) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, month);
return cal.getTime();
}
/**
* 取得n天前时间
*/
public static String getTimeByDay(String fromat, String dateBefore, int day) {
SimpleDateFormat sdf = new SimpleDateFormat(fromat);
String tomorrow = "";
try {
Date myDate = sdf.parse(dateBefore);
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
cal.add(Calendar.DATE, day);
tomorrow = new SimpleDateFormat(fromat).format(cal.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tomorrow;
}
/**
* 取得n天前时间
*/
public static Date getTimeByDay(Date dateBefore, int min) {
Calendar cal = Calendar.getInstance();
cal.setTime(dateBefore);
cal.add(Calendar.DATE, min);
return cal.getTime();
}
/**
* 返回某个日期后的第一个n号的日期
* @param date 日期
* @param n n号
* @return java.util.Date
**/
public static Date getFirstMonthDay(Date date, Integer n) {
Calendar c = Calendar.getInstance();
c.setTime(date);
Integer year = c.get(Calendar.YEAR);
Integer month = c.get(Calendar.MONTH);
Integer day = c.get(Calendar.DATE);
Integer diff = n-day;
if (diff<0){
month++;
c.set(year,month,n);
Integer firstDay = c.get(Calendar.DATE);
while (!firstDay.equals(n)) {
c.set(year,month++,n);
firstDay = c.get(Calendar.DATE);
}
return c.getTime();
}
c.set(year,month,n);
Integer firstDay = c.get(Calendar.DATE);
while (!firstDay.equals(n) || c.getTime().before(date)) {
c.set(year,month++,n);
firstDay = c.get(Calendar.DATE);
}
return c.getTime();
}
/**
* 返回某个日期前的第一个n号的日期
* @param date 日期
* @param n n号
* @return java.util.Date
**/
public static Date getLastMonthDay(Date date, Integer n) {
Integer day = Integer.parseInt(String .format("%td", date));
Integer diff = n-day;
if (diff>0){
Date firstMonth = getTimeByMonth(date,-1);
return getTimeByDay(firstMonth,diff);
}
return getTimeByDay(date,diff);
}
/**
* 返回某个日期的第一个星期n的日期
* @param date 日期
* @param n 新期n
* @return java.util.date
**/
public static Date getFirstWeekDay(Date date, Integer n) {
Integer nowWeekDay = getWeekDayOfDate(date);
Double fewDay = ((nowWeekDay-n)/Math.abs(nowWeekDay-n)*0.5+0.5)*7+n-nowWeekDay;
return getTimeByDay(date,fewDay.intValue());
}
/**
* 返回某个日期的上一个星期n的日期
* @param date 日期
* @param n 新期n
* @return java.util.date
**/
public static Date getLastWeekDay(Date date, Integer n) {
Integer nowWeekDay = getWeekDayOfDate(date);
Double fewDay = ((nowWeekDay-n)/Math.abs(nowWeekDay-n)*0.5-0.5)*7+n-nowWeekDay;
return getTimeByDay(date,fewDay.intValue());
}
/**
* 取得n小时前时间
*/
public static Date getTimeByHour(Date dateBefore, int min) {
Calendar cal = Calendar.getInstance();
cal.setTime(dateBefore);
cal.add(Calendar.HOUR, min);
return cal.getTime();
}
/**
* 取得n分钟前时间
*/
public static String getTimeByMinute(String fromat, String dateBefore, int min) {
SimpleDateFormat sdf = new SimpleDateFormat(fromat);
String tomorrow = "";
try {
Date myDate = sdf.parse(dateBefore);
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
cal.add(Calendar.MINUTE, min);
tomorrow = new SimpleDateFormat(fromat).format(cal.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tomorrow;
}
/**
* 取得n分钟前时间
*/
public static Date getTimeByMinute(Date dateBefore, int min) {
Calendar cal = Calendar.getInstance();
cal.setTime(dateBefore);
cal.add(Calendar.MINUTE, min);
return cal.getTime();
}
/**
* 取得n秒前时间
* @throws ParseException
*/
public static Date getTimeBySecond(Date dateBefore, int second) {
Calendar cal = Calendar.getInstance();
cal.setTime(dateBefore);
cal.add(Calendar.SECOND, second);
return cal.getTime();
}
/**
* 取得n秒前时间
* @throws ParseException
*/
public static String getTimeBySecond(String fromat, String dateBefore, int second) {
SimpleDateFormat sdf = new SimpleDateFormat(fromat);
String tomorrow = "";
try {
Date myDate = sdf.parse(dateBefore);
Calendar cal = Calendar.getInstance();
cal.setTime(myDate);
cal.add(Calendar.SECOND, second);
tomorrow = new SimpleDateFormat(fromat).format(cal.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tomorrow;
}
/**
* 取得周几
* @throws ParseException
*/
public static Integer getWeekDayOfDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)-1;
return dayOfWeek;
}
/**
* 日期变为字符串
*/
public static String dateToString(Date date, String iso) {
SimpleDateFormat format = new SimpleDateFormat(iso);
return format.format(date);
}
/**
* 日期变为字符串
*/
public static Date dayOfDate(Date date) {
String dateToString = dateToString(date, SHORT_DATE_FORMAT);
return stringToDate(dateToString,SHORT_DATE_FORMAT);
}
/**
* 把Date格式化成yyyy-MM-dd HH:mm:ss
*
* @param dt
* @return
*/
public static String formatLongDate(Date dt) {
SimpleDateFormat sdf = new SimpleDateFormat(LONG_DATE_FORMAT);
if (dt == null) {
return null;
}
return sdf.format(dt);
}
/**
* 把Date格式化
*
* @param dt
* @return
*/
public static String formatLongDate(Date dt, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
if (dt == null) {
return null;
}
return sdf.format(dt);
}
/**
* 获取时间差
* @param firstTime
* @param endTime
* @return 毫秒
*/
public static String betweenTime(String firstTime, String endTime) {
SimpleDateFormat dfs = new SimpleDateFormat(LONG_DATE_FORMAT0);
long between = 0;
try {
Date first = dfs.parse(firstTime);
Date end = dfs.parse(endTime);
between = (end.getTime() - first.getTime());// 得到两者的毫秒数
} catch (Exception ex) {
ex.printStackTrace();
}
return between + "";
}
/**
* 返回当前时间在起止时间的位置
* @param localTime 当前时间
* @param firstTime 开始时间
* @param endTime 结束时间
* @return int -1:早于开始时间
* 0:在开始结束中间
* 1:晚于结束时间
*/
public static int betweenAt(Date localTime, Date firstTime, Date endTime) {
if (localTime.before(firstTime)) {
return -1;
}
if (localTime.after(endTime)) {
return 1;
}
return 0;
}
/**
* 获取时间差
* @param firstTime
* @param endTime
* @return 毫秒
*/
public static long betweenTime(Date firstTime, Date endTime) {
long between = (firstTime.getTime() - endTime.getTime());// 得到两者的毫秒数
return between;
}
/**
* 比较时间大小
* @param d1
* @param d2
* @return 大的
*/
public static String compareDateReturnBig(String d1, String d2) {
Date dt1 = null;
Date dt2 = null;
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.LONG_DATE_FORMAT);
try {
dt1 = sdf.parse(d1);
} catch (Exception e) {
return d2;
}
try {
dt2 = sdf.parse(d2);
} catch (Exception e) {
return d1;
}
if (dt1.getTime() > dt2.getTime()) {
return d1;
} else if (dt1.getTime() < dt2.getTime()) {
return d2;
} else {
return d1;
}
}
/**
* 比较时间大小
* @param d1
* @param d2
* @return 大的
*/
public static Date compareDateReturnBig(Date d1, Date d2) {
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.LONG_DATE_FORMAT);
if (d1.getTime() > d2.getTime()) {
return d1;
} else if (d1.getTime() < d2.getTime()) {
return d2;
} else {
return d1;
}
}
/**
* 比较时间大小
* @param d 天
* @param time 时间
* @return 大的
*/
public static Date getDateTime(Date d, String time) {
String date = dateToString(d,SHORT_DATE_FORMAT);
String dateTime = date+" "+time;
return stringToDate(DateUtil.LONG_DATE_FORMAT,dateTime);
}
/**
* 比较时间大小
* @param d 天
* @param t 时间
* @return 大的
*/
public static Date getDateTime(Date d, Date t) {
String date = dateToString(d,SHORT_DATE_FORMAT);
String time = dateToString(t,SHORT_TIME_FORMAT);
String dateTime = date+" "+time;
return stringToDate(DateUtil.LONG_DATE_FORMAT,dateTime);
}
/**
* 返回两个时间相同部分
*
* @param time1
* @param time2
* @return 毫秒
*/
public static String getSamePart(String time1, String time2) {
String msg = "";
try {
int length = time1.length()>time2.length()?time2.length():time1.length();
for (int i = 0; i < length; i++) {
if (time1.charAt(i) == time2.charAt(i)) {
msg += time1.charAt(i) + "";
} else {
break;
}
}
}catch (Exception e) {
log.error(e.getMessage(), e);
}
return msg;
}
public static String getCurrent(String pattern) {
return DateTime.now().toString(pattern);
}
public static String getCurrent() {
return getCurrent("yyyyMMddHHmmss");
}
public static void main(String[] args) {
// Date date = new Date();
// Integer day = getWeekDayOfDate(date);
// Date firstWeekDay = getFirstWeekDay(date,1);
// Date lastWeekDay = getLastWeekDay(date,0);
//// Date firstMonthDay = getFirstMonthDay(date,31);
// Date lastMonthDay = getLastMonthDay(date,31);
// String c = getSamePart("2017-05-31", "2017-05-31");
//
// System.out.println("===------------------" + c);
Date oldMonthDay = stringToDate(LONG_DATE_FORMAT,"2019-12-22 10:10:32");
Date firstMonthDay = getFirstMonthDay(oldMonthDay,22);
System.out.println(firstMonthDay);
}
}