我们在实际开发过程中会经常遇到需要处理日期时间的需求,我在这里整理了一下我常用到的日期时间的函数,包括
格式化日期,计算两个日期现相差多少天,计算几天后的日期,计算几天前的日期,两个日期中的每一天,获取日期月份的第一天、最后一天,获取日期是星期几
的方法,后期会持续更新
/**
* Date format pattern this is often used.
*/
public static final String PATTERN_YMD = "yyyy-MM-dd";
/**
* Date format pattern this is often used.
*/
public static final String PATTERN_YMDHMS="yyyy-MM-dd HH:mm:ss";
/**
* 由字符串时间得到Date类型时间
*
* @param strDate
* @param pattern
* @return
*/
public static Date getDateFomat(String strDate, String pattern) throws ParseException {
if(StringUtils.isEmptyOrNull(strDate)||StringUtils.isEmptyOrNull(pattern)){
throw new IllegalArgumentException("strDate or pattern is null");
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = sdf.parse(strDate);
return date;
}
/**
* 由Date类型时间得到字符串时间
*
* @param date
* @param pattern
* @return
*/
public static String getDateStrFomat(Date date, String pattern) {
Format formatter = new SimpleDateFormat(pattern);
return formatter.format(date);
}
/**
* 得到几天前的时间
*
* @param date
* @param day
* @return
*/
public static String getDateBefore(Date date, int day) {
Calendar now = Calendar.getInstance();
now.setTime(date);
now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
Format formatter = new SimpleDateFormat(PATTERN_YMD);
return formatter.format(now.getTime());
}
/**
* 得到几天后的时间
*
* @param date
* @param day
* @return
*/
public static String getDateAfter(Date date, int day) {
Calendar now = Calendar.getInstance();
now.setTime(date);
now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
Format formatter = new SimpleDateFormat(PATTERN_YMD);
return formatter.format(now.getTime());
}
/**
* 获取日期月份的第一天,如果传空则获取当前月份的第一天
*
* @param strDate
* @throws ParseException
*/
public static String getMonthFirstDay(String strDate) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(PATTERN_YMD);
Calendar cal = Calendar.getInstance();//获取当前日期
if (!StringUtils.isEmptyOrNull(strDate)) {
Date parseDate = format.parse(strDate);
cal.setTime(parseDate);
}
cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
String monthFirstDay = format.format(cal.getTime());
return monthFirstDay;
}
/**
* 获取日期月份的最后一天,如果传空则获取当前月份
*
* @param strDate
* @throws ParseException
*/
public static String getMonthLastDay(String strDate) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(PATTERN_YMD);
Calendar cal = Calendar.getInstance();//获取当前日期
if (!StringUtils.isEmptyOrNull(strDate)) {
Date parseDate = format.parse(strDate);
cal.setTime(parseDate);
}
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
String monthLastDay = format.format(cal.getTime());
return monthLastDay;
}
/**
* 获取两个日期之间相隔多少天
*
* @param strDate1
* @param strDate2
* @return
* @throws ParseException
*/
public static Long getBetweenDay(String strDate1, String strDate2) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(PATTERN_YMD);
Date date1 = format.parse(strDate1);
Date date2 = format.parse(strDate2);
Long inteverDay = (date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24);
return inteverDay > 0 ? inteverDay : -inteverDay;
}
/**
* 获取几天后的日期
*
* @param strDate
* @param days
* @throws ParseException
*/
public static String addCurrentDay(String strDate, int days) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(PATTERN_YMD);
Date parseDate = format.parse(strDate);
Calendar cal = Calendar.getInstance();//获取当前日期
cal.setTime(parseDate);
cal.add(Calendar.DAY_OF_YEAR, +days);
String currentDay = format.format(cal.getTime());
return currentDay;
}
/**
* 获取几天前的日期
*
* @param strDate
* @param days
* @return
* @throws ParseException
*/
public static String reduceCurrentDay(String strDate, int days) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(PATTERN_YMD);
Date parseDate = format.parse(strDate);
Calendar cal = Calendar.getInstance();//获取当前日期
cal.setTime(parseDate);
cal.add(Calendar.DAY_OF_YEAR, -days);
String currentDay = format.format(cal.getTime());
return currentDay;
}
/**
* 获取两个日期之间de所有天数
*
* @param strDate1
* @param strDate2
* @return
*/
public static ArrayList<String> getCalendar(String strDate1, String strDate2) throws ParseException {
ArrayList<String> calendarList = new ArrayList<>();
SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd");
Date start = format.parse(strDate1);
Date end = format.parse(strDate2);
// start = new Date(start.getTime() + 86400000);
while (start.getTime() <= end.getTime()) {
calendarList.add(format.format(start));
start = new Date(start.getTime() + 1000 * 60 * 60 * 24);
}
return calendarList;
}
/**
* 获取当前日期是星期几
*
* @param strDate
* @return
*/
public static String getWeekDay(String strDate) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(PATTERN_YMD);
Date date = format.parse(strDate);
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];
}