日期工具类
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
public class MyDateUtils {
private MyDateUtils() {
throw new IllegalStateException("MyDateUtils class");
}
public static final String DATE_FORMAT_YMD = "yyyy-MM-dd";
public static final String DATE_FORMAT_YMD_HM = DATE_FORMAT_YMD + " HH:mm";
public static final String DATE_FORMAT_YMD_HMS = DATE_FORMAT_YMD + " HH:mm:ss";
public static final String DATE_FORMAT_YMDHMS = "yyyyMMddHHmmss";
/**
* 获取被月偏移1号零时零分零秒
* @param curDay Date
* @param offset int
* @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
* @return
*/
public static String getFirstZerotimeDayByMonthoffset(Date curDay, int offset, String formatStr) {
return new SimpleDateFormat(formatStr).format(getFirstZerotimeDayByMonthoffset(curDay, offset));
}
public static Date getFirstZerotimeDayByMonthoffset(Date curDay, int offset) {
Calendar cal = Calendar.getInstance();
cal.setTime(curDay);
if (offset != 0) {
cal.add(Calendar.MONTH, offset);
}
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal.getTime();
}
/**
* 获取被日偏移某天的零时零分零秒
* @param curDay Date
* @param offset int
* @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
* @return
*/
public static String getZerotimeDayByDayoffset(Date curDay, int offset, String formatStr) {
return new SimpleDateFormat(formatStr).format(getZerotimeDayByDayoffset(curDay, offset));
}
public static Date getZerotimeDayByDayoffset(Date curDay, int offset) {
Calendar cal = Calendar.getInstance();
cal.setTime(curDay);
if (offset != 0) {
cal.add(Calendar.DAY_OF_MONTH, offset);
}
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal.getTime();
}
/**
* 获取被日偏移某天的23时59分59秒
* @param curDay Date
* @param offset int
* @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
* @return
*/
public static String getEndtimeDayByDayoffset(Date curDay, int offset, String formatStr) {
return new SimpleDateFormat(formatStr).format(getEndtimeDayByDayoffset(curDay, offset));
}
public static Date getEndtimeDayByDayoffset(Date curDay, int offset) {
Calendar cal = Calendar.getInstance();
cal.setTime(curDay);
if (offset != 0) {
cal.add(Calendar.DAY_OF_MONTH, offset);
}
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}
/**
* 获取被日偏移的某天
* @param curDay Date
* @param offset int
* @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
*/
public static String getDayByDayoffset(Date curDay, int offset, String formatStr) {
return new SimpleDateFormat(formatStr).format(offset == 0 ?curDay :getDayByDayoffset(curDay, offset));
}
public static Date getDayByDayoffset(Date curDay, int offset) {
Calendar cal = Calendar.getInstance();
cal.setTime(curDay);
if (offset != 0) {
cal.add(Calendar.DAY_OF_MONTH, offset);
}
return cal.getTime();
}
/**
* 获取被小时偏移的某天
* @param curDay Date
* @param offset int
* @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
*/
public static String getDayByHourOffset(Date curDay, int offset, String formatStr) {
return new SimpleDateFormat(formatStr).format(offset == 0 ?curDay :getDayByHourOffset(curDay, offset));
}
public static Date getDayByHourOffset(Date curDay, int offset) {
Calendar cal = Calendar.getInstance();
cal.setTime(curDay);
if (offset != 0) {
cal.add(Calendar.HOUR_OF_DAY, offset);
}
return cal.getTime();
}
/**
* 获取被秒偏移的某天
* @param curDay Date
* @param offset int
* @param formatStr "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
* @return
*/
public static String getDayBySecoundoffset(Date curDay, int offset, String formatStr) {
return new SimpleDateFormat(formatStr).format(offset == 0 ?curDay :getDayBySecoundoffset(curDay, offset));
}
public static Date getDayBySecoundoffset(Date curDay, int offset) {
Calendar cal = Calendar.getInstance();
cal.setTime(curDay);
if (offset != 0) {
cal.add(Calendar.SECOND, offset);
}
return cal.getTime();
}
/**
* 字串转日期(零时零分零秒)
* @param dateStr
* @param format "yyyy-MM-dd/yyyyMMddHHmmss/yyyy-MM-dd HH:mm:ss"
* @return
*/
public static Date convertStr2Date(String dateStr, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(StringUtils.isNotBlank(format) ?format :DATE_FORMAT_YMDHMS);
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 日期比较
* @param date1 String
* @param date2 String
* @return -2:异常; 1:大于; 0:等于; -1:小于
*/
public static int compareDate(String date1, String date2) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YMD);
if (StringUtils.isNotBlank(date1) && StringUtils.isNotBlank(date2)) {
try {
long dl1 = sdf.parse(date1).getTime();
long dl2 = sdf.parse(date2).getTime();
if (dl1 > dl2) {
return 1;
} else if (dl1 == dl2) {
return 0;
} else {
return -1;
}
} catch (ParseException e) {
e.printStackTrace();
}
}
return -2;
}
/**
* 获取当前时间戳,单位秒
* @return long
*/
public static long getCurrentTimestamp() {
return System.currentTimeMillis() / 1000;
}
/**
* 日期格式
* @param date
* @param format
* @return
*/
public static String date2Format(Date date, String format) {
return new SimpleDateFormat(format).format(date);
}
}