public class DateUtils {
/**
* 日期
/
public static final String YEAR_TO_DAY = “yyyy-MM-dd”;
/*
* 24小时制,精确到秒
/
public static final String YEAR_TO_SEC = “yyyy-MM-dd HH:mm:ss”;
/*
* 24小时制,精确到毫秒
/
public static final String YEAR_TO_MS = “yyyy-MM-dd HH:mm:ss.SSS”;
/*
* 24小时制,精确到分
*/
public static final String YEAR_TO_MINUTE = “yyyy-MM-dd HH:mm”;
public static final String MONTH_TO_MINUTE = "MM-dd HH:mm";
public static final String HOUR_TO_SEC = "HH:mm:ss";
public static final String YEAR_TO_MS_UN_LINE = "yyyyMMdd HHmmssSSS";
public static final String YEAR_TO_SEC_UN_LINE = "yyyyMMdd HHmmss";
public static final String YEAR_TO_MI_UN_LINE = "yyyyMMdd HHmm";
public static final String YEAR_TO_DAY_UN_LINE = "yyyyMMdd";
public static final String YEAR_TO_HOUR_UN_LINE = "yyyyMMddHH";
public static final String YEAR_TO_MS_NO_BLANK = "yyyyMMddHHmmssSSS";
public static final String YEAR_TO_SEC_NO_BLANK = "yyyyMMddHHmmss";
public static final String YEAR_TO_MI_NO_BLANK = "yyyyMMddHHmm";
public static final String DAY_TO_MINUTE = "dd HH:mm";
public static final String YEAR_TO_MINUTE_UN_LINE = "yyyy-MM-dd HHmm";
public static final String TRAVELSKY_IBE_FORMAT = "yyyyMMdd HH:mm:ss";
public static final String YEAR_TO_MINUTE_LINK = "yyyyMMdd-HHmm";
public static final String YEAR_TO_MS_GMT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public static final String YEAR_TO_SEC_GMT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
public static final String YEAR = "yyyy";
public static final String YEAR_TO_MONTH_UN_LINE = "yyyyMM";
private static final ILogger log = LoggerFactory.getLogger(DateUtils.class);
/**
* 转换成中文的星期几
*
* @param day
* @return
*/
public static String getChineseDayOfWeek(int day) {
String ret = "";
switch (day) {
case 1:
ret = "星期日";
break;
case 2:
ret = "星期一";
break;
case 3:
ret = "星期二";
break;
case 4:
ret = "星期三";
break;
case 5:
ret = "星期四";
break;
case 6:
ret = "星期五";
break;
case 7:
ret = "星期六";
break;
default:
return "error";
}
return ret;
}
/**
* 转换成中文的星期几
*
* @param date
* @return
*/
public static String getChineseDayOfWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_WEEK);
return getChineseDayOfWeek(day);
}
/**
* 转换成中文的周几
*
* @param day
* @return
*/
public static String getChineseDayOfWeekOFzhou(int day) {
String ret = "";
switch (day) {
case 1:
ret = "周日";
break;
case 2:
ret = "周一";
break;
case 3:
ret = "周二";
break;
case 4:
ret = "周三";
break;
case 5:
ret = "周四";
break;
case 6:
ret = "周五";
break;
case 7:
ret = "周六";
break;
default:
return "error";
}
return ret;
}
/**
* 转换成中文的周几
*
* @param date
* @return
*/
public static String getChineseDayOfWeekOfZhou(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_WEEK);
return getChineseDayOfWeekOFzhou(day);
}
/**
* 转换成日期类型
*
* @param dateStr 日期字符串
* @param format 日期字符串的格式
* @return
* @throws ParseException
*/
public static Date format(String dateStr, String format) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date date = formatter.parse(dateStr);
return date;
}
/**
* 转换成日期字符串
*
* @param date 日期类型
* @param format 需要输出的格式
* @return
* @throws ParseException
*/
public static String format(Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
String dateStr = formatter.format(date);
return dateStr;
}
/**
* 转换成日期字符串 兼容JDK1.5
*
* @param date
* @param format
* @return
*/
public static String dateFormatStr(Date date, String format) {
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
SimpleDateFormat sdf = new SimpleDateFormat(format);
if(date == null){
return "";
}
return sdf.format(date);
}
/**
* 增加年份
* @param date
* @param year
* @return
*/
public static Date addYear(Date date, int year) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.YEAR, year);
return c.getTime();
}
/**
* 日期增加月
* @param date
* @param month
* @return
*/
public static Date addMonth(Date date, int month) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, month);
return c.getTime();
}
/**
* 日期增加天
*
* @param date 日期
* @param day 天数
* @return
*/
public static Date addDate(Date date, int day) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
return c.getTime();
}
/**
* 日期增加小时
*
* @param date 日期
* @param hour 小时数
* @return
*/
public static Date addHour(Date date, int hour) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.HOUR_OF_DAY, hour);
return c.getTime();
}
/**
* 日期增加分钟
*
* @param date 日期
* @param minute 分钟数
* @return
*/
public static Date addMinute(Date date, int minute) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MINUTE, minute);
return c.getTime();
}
/**
* 日期增加秒
*
* @param date 日期
* @param second 秒数
* @return
*/
public static Date addSecond(Date date, int second) {
if (date == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.SECOND, (second));
return c.getTime();
}
/**
* 增加耗秒数
* @param date
* @param milliSecond
* @return
*/
public static Date addMilliSecond(Date date, int milliSecond) {
if (date == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MILLISECOND, (milliSecond));
return c.getTime();
}
/**
* 返回毫秒
*
* @param date 日期
* @return
*/
public static long getMillis(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
/**
* 返回毫秒
*
* @param dateStr 日期
* @return
*/
public static long getMilliSecond(String dateStr, String format) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(format);
Date date = formatter.parse(dateStr);
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
/**
* 字符串转为日期
*
* @param dateStr
* @param format
* @return
*/
public static Date parseDate(final String dateStr, String format) throws B2CException {
if(StringUtils.isEmpty(dateStr)){
return null;
}
if(format == null){
format = "yyyy-MM-dd HH:mm:ss";
}
Date date = null;
try {
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
String dt = dateStr;
if ((!"".equals(dt)) && (dt.length() < format.length())) {
dt += format.substring(dt.length()).replaceAll("[YyMmDdHhSs]", "0");
}
date = (Date) df.parse(dt);
} catch (Exception e) {
log.errorCode(MsgErrorCode.TRA_B_SYSTEM_ERROR.getCode()).error(e.getMessage(),e);
throw new B2CException(MsgErrorCode.TRA_B_SYSTEM_ERROR, e);
}
return date;
}
/**
* 判断日期是否在两个日期之内
*
* @param nowDate 要比较的日期
* @param beginDate 开始日期
* @param endDate 结束日期
*/
public static boolean comparisonDate(Date nowDate, Date beginDate, Date endDate) {
boolean bool = false;
if (null == nowDate || null == beginDate || null == endDate) {
return bool;
}
long nowTime = nowDate.getTime();
long beginTime = beginDate.getTime();
long endTime = endDate.getTime();
if (nowTime >= beginTime && nowTime <= endTime) {
bool = true;
}
return bool;
}
/**
* 两个时间差距多少秒
*
* @return
*/
public static long getDateMinus(Date end, Date begin) {
return (end.getTime() - begin.getTime()) / 1000;
}
/**
* 将字符串转为日期
*
* @param dateStr
* @param formatStr
* @return
* @throws ParseException
*/
public static Date parseDateByGMT(String dateStr, String formatStr) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(formatStr);
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(8, "GMT")));
Date date = format.parse(dateStr);
return date;
}
/**
* 将日期转为GMT格式字符串
*
* @param date
* @return
* @throws ParseException
*/
public static String formatDateByGMT(Date date) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(YEAR_TO_MS_GMT);
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
String str = format.format(date);
return str;
}
/**
* 将GMT格式日期转为日期
*
* @param date
* @return
* @throws ParseException
*/
public static Date parseDateByGMT(Date date) throws B2CException {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.CHINESE);
Calendar day = Calendar.getInstance();
cal.setTime(date);
day.set(Calendar.YEAR, cal.get(Calendar.YEAR));
day.set(Calendar.MONTH, cal.get(Calendar.MONTH));
day.set(Calendar.DATE, cal.get(Calendar.DATE));
day.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
day.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
day.set(Calendar.SECOND, cal.get(Calendar.SECOND));
Date gmt8Date = day.getTime();
return gmt8Date;
}
/**
* 日期相减
*
* @param date 日期
* @param hour 小时
* @return 返回相减后的日期
*/
public static Date diffDateByHour(Date date, int hour) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(getMillis(date) - ((long) hour) * 3600 * 1000);
return c.getTime();
}
/**
* 日期相减
*
* @param date 日期
* @param day 天数
* @return 返回相减后的日期
*/
public static Date diffDateByDay(Date date, int day) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(getMillis(date) - ((long) day) * 24 * 3600 * 1000);
return c.getTime();
}
/**
* 根据时间获取时间差(**小时**分钟)
*
* @param begin
* @param end
* @return
*/
public static String getDuringTimeString(Date begin, Date end) {
int duringHour = (int) (end.getTime() - begin.getTime()) / (1000 * 60 * 60);
int duringMinute = (int) ((end.getTime() - begin.getTime()) % (1000 * 60 * 60)) / (1000 * 60);
return duringHour + "小时" + duringMinute + "分钟";
}
/**
* 根据时间获取时间差(分钟)
*
* @param begin
* @param end
* @return
*/
public static int getDiffTimeMinute(Date begin, Date end) {
int duringMinute = (int) (end.getTime() - begin.getTime()) / (1000 * 60);
return duringMinute;
}
/**
* 根据时间获取天数差
*
* @param begin
* @param end
* @return
*/
public static int getDiffTimeDate(Date begin, Date end) {
int duringDay = (int) (end.getTime() / (1000 * 60 * 60 * 24)
- begin.getTime() / (1000 * 60 * 60 * 24));
return duringDay;
}
/**
* 根据时间获取时间差(**小时**分钟)
*
* @param begin
* @param end
* @param format
* @return
* @throws ParseException
*/
public static String getDuringTimeString(String begin, String end, String format) throws ParseException, B2CException {
Date beginDate = format(begin, format);
Date endDate = format(end, format);
int duringHour = (int) (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60);
int duringMinute = (int) ((endDate.getTime() - beginDate.getTime()) % (1000 * 60 * 60)) / (1000 * 60);
return duringHour + "小时" + duringMinute + "分钟";
}
public static String format(Calendar date, String format) throws ParseException, B2CException {
String dateStr = format(date.getTime(), format);
return dateStr;
}
/**
* 在Date基础上加day天,可以加可减,如果day为负数,则为减多少天
*
* @param date
* @param day
* @return
*/
@SuppressWarnings("static-access")
public static Date addDateByDay(Date date, int day) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
// 把日期往后增加一天.整数往后推,负数往前移动
calendar.add(Calendar.DATE, day);
date = calendar.getTime();
return date;
}
/**
* 计算两个日期的差
*
* @param start
* @param end
* @return
*/
public static long getDateDiffer(Date start, Date end, TimeUnit tu) {
long e = end.getTime();
long s = start.getTime();
return tu.convert(e - s, TimeUnit.MILLISECONDS);
}
/**
* 在time 的前X天
*
* @param flyTime
* @param day
* @return
*/
public static boolean isBeforeXDays(Date flyTime, int day) {
Date now = new Date();
if (now.getTime() <= addDate(flyTime, day).getTime()) {
return true;
}
return false;
}
/**
* 获取与当前时间相距的时间
*
* @param time
* @param format
* @return
* @throws ParseException
*/
public static Long getleftNowTime(String time, String format) throws ParseException, B2CException {
Long leftTime = null;
Date beginDate = DateUtils.format(time, format);
leftTime = Math.abs(beginDate.getTime() - System.currentTimeMillis());
return leftTime;
}
public static Long getleftNowTime(Date time) throws ParseException {
Long leftTime = null;
leftTime = Math.abs(time.getTime() - System.currentTimeMillis());
return leftTime;
}
/**
* 改变日期的格式
*
* @param oldFormat
* @param newFormat
* @param dateStr
* @return
* @throws ParseException
*/
public static String changeDateFormat(String oldFormat, String newFormat, String dateStr) throws ParseException {
SimpleDateFormat oldFormatter = new SimpleDateFormat(oldFormat);
SimpleDateFormat newFormatter = new SimpleDateFormat(newFormat);
Date tranDate = oldFormatter.parse(dateStr);
String newDateStr = newFormatter.format(tranDate);
return newDateStr;
}
/**
* 改变日期的格式
*
* @param oldFormat
* @param newforFormat
* @param dateStr
* @return
* @throws ParseException
*/
public static String changeDateFormat(SimpleDateFormat oldFormat, SimpleDateFormat newforFormat, String dateStr) throws ParseException {
Date tranDate = oldFormat.parse(dateStr);
String newDateStr = newforFormat.format(tranDate);
return newDateStr;
}
/**
* 将时间戳转为日期格式
* @param time
* @param format
* @return
* @throws ParseException
*/
public static Date paseDateByLongTime(Long time,String format) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(format);
String timeStr = formatter.format(time);
Date date = formatter.parse(timeStr);
return date;
}
/**
* 获取某天的00时00分00秒
* @param date
* @return
*/
public static Date getDayInitialTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 获取某天的23时59分59秒
* @param date
* @return
*/
public static Date getDayEndTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
/**
* 获取某时的00分00秒
* @param date
* @return
*/
public static Date getHourInitialTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 获取某时的59分59秒
* @param date
* @return
*/
public static Date getHourEndTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
public static Date truncate(Date date , int field){
return org.apache.commons.lang3.time.DateUtils.truncate(date , field);
}
public static Calendar toCalendar(Date date ){
return org.apache.commons.lang3.time.DateUtils.toCalendar(date);
}
}