import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class DateUtils {
/** 日志 */
private static Logger log = LoggerFactory.getLogger(DateUtils.class);
/** yyyy-MM-dd. */
public static final String DATE_FORMAT_ONE = "yyyy-MM-dd";
/** yyyy-MM. */
public static final String DATE_FORMAT = "yyyy-MM";
/** yyyy/MM/dd. */
public static final String DATE_FORMAT_TWO = "yyyy/MM/dd";
/** yyyyMMdd. */
public static final String DATE_FORMAT_THREE = "yyyyMMdd";
/** yyyyMM. */
public static final String DATE_FORMAT_FOUR = "yyyyMM";
/** yyyy-MM-dd HH:mm:ss. */
public static final String DATE_FORMAT_FIVE = "yyyy-MM-dd HH:mm:ss";
/** yyyy-MM-dd HH:mm. */
public static final String DATE_FORMAT_FIVE_1 = "yyyy-MM-dd HH:mm";
/** yyyy-MM-dd HH:mm:ss. */
public static final String DATE_FORMAT_UPDATE = "yyyy-MM-dd HH:mm:ss.SSS";
/** yyyy. */
public static final String DATE_FORMAT_SIX = "yyyy";
/** MM. */
public static final String DATE_FORMAT_SEVEN = "MM";
/** yyyy/MM/dd HH:mm:ss. */
public static final String DATE_FORMAT_EIGHT = "yyyy/MM/dd HH:mm:ss";
/** yyyy/MM/dd HH:mm:ss. */
public static final String DATE_FORMAT_AEIGHT = "dd/MM/yyyy HH:mm:ss";
/** HH:mm:ss. */
public static final String DATE_FORMAT_NINE = "HH:mm:ss";
/** yy/MM/dd. */
public static final String DATE_FORMAT_TEN = "yy/MM/dd";
/** yyyy/M/d. */
public static final String DATE_FORMAT_ELEVEN = "yyyy/M/d";
/** yyyy/MM. */
public static final String DATE_FORMAT_TWELEVE = "yyyy/MM";
/** yyyyMMddHHmmssSSS. */
public static final String DATE_FORMAT_THIRTEEN = "yyyyMMddHHmmssSSS";
/** yyMMdd. */
public static final String DATE_FORMAT_FOURTEEN = "yyMMdd";
/** yyyy.MM.dd. */
public static final String DATE_FORMAT_SIXTEEN = "yyyy.MM.dd";
/** yyyyMMddHHmmss. */
public static final String DATE_FORMAT_SEVENTEEN = "yyyyMMddHHmmss";
/** yyyyMMddHHmmss. */
public static final String DATE_FORMAT_SEVENTEEN1 = "yyyyMMddHHmm";
/** yyyy年MM月dd日 */
public static final String DATE_FORMAT_EIGHTEEN = "yyyy年MM月dd日";
/** HH:mm. */
public static final String DATE_FORMAT_NINETEEN = "HH:mm";
/** HHmm. */
public static final String DATE_FORMAT_TWENTY = "HHmm";
/** MMM d'th', yyyy EEE'.' */
public static final String DATE_FORMAT_THIRTY = "EEE', 'MMM d'th', yyyy'.'";
/** yyyy'年'MM'月'dd日, EEE */
public static final String DATE_FORMAT_FORTY = "yyyy'年'MM'月'dd'日, 'EEE";
/** MM/dd/yy */
public static final String DATE_FORMAT_FIFTY = "MM/dd/yy";
/** MM/dd/yy HH:mm:ss */
public static final String DATE_FORMAT_FIFTY_ONE = "MM/dd/yy HH:mm:ss";
/** MM/dd/yy|HH:mm:ss */
public static final String DATE_FORMAT_FIFTY_TWO = "MM/dd/yy|HH:mm:ss";
/** INT_4. */
private static final int INT_4 = 4;
/** INT_6. */
private static final int INT_6 = 6;
/** INT_8. */
private static final int INT_8 = 8;
/** . */
public static final String YEAR_FORMAT = "[12][0-9]{3}";
/** 年. */
public static final int YEAR = 1;
/** 月. */
public static final int MONTH = 2;
/** 日. */
public static final int DAY = 3;
/** 开始时间 */
private static final String START_TIME = " 00:00:00";
/** 结束时间 */
private static final String END_TIME = " 23:59:59";
/**
* 按指定格式转化字符串为Date
*
* @param strDate
* String
* @param strFormat
* String
*
* @return FormatDate
*
* @throws java.text.ParseException
* ParseException
*/
public static Date getFormatDate(String strDate, String strFormat) {
if (StringUtil.isEmpty(strDate) || StringUtil.isEmpty(strFormat)) {
return null;
}
SimpleDateFormat df = new SimpleDateFormat(strFormat);
df.setLenient(false);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
log.error("ParseException",e);
return null;
}
}
/**
* 按指定格式转化Date为字符串
*
* @param date
* Date
* @param toFormat
* String
*
* @return FormatDate String
*/
public static String dateToString(Date date, String toFormat) {
if (date == null) {
return "";
}
SimpleDateFormat df = new SimpleDateFormat(toFormat);
df.setLenient(false);
return df.format(date);
}
/**
* 按指定格式转化Date为字符串
*
* @param date
* Date
* @param strFormat
* String
*
* @return FormatDate String
*/
public static Date formatDate(Date date, String strFormat) {
if (date == null) {
return null;
}
Date reDate = null;
reDate = getFormatDate(dateToString(date, strFormat), strFormat);
return reDate;
}
/**
* <PRE>
*
* 比较两个日期
*
* </PRE>
*
* @param day1
* String
* @param day2
* String
* @return int day1>day2返回1,day1<day2返回-1,day1=day2返回0
*/
public static int compareDate(String day1, String day2) {
int isEarly = -1;
if (day1 == null || day1.trim().equals("") || day2 == null || day2.trim
().equals("")) {
isEarly = -1;
} else {
day1 = day1.replaceAll("-", "");
day2 = day2.replaceAll("-", "");
int year1 = Integer.parseInt(day1.substring(0, INT_4));
int month1 = Integer.parseInt(day1.substring(INT_4, INT_6)) - 1;
int day = Integer.parseInt(day1.substring(INT_6, INT_8));
Calendar cal1 = Calendar.getInstance();
cal1.set(year1, month1, day);
int year2 = Integer.parseInt(day2.substring(0, INT_4));
int month2 = Integer.parseInt(day2.substring(INT_4, INT_6)) - 1;
int day22 = Integer.parseInt(day2.substring(INT_6, INT_8));
Calendar cal2 = Calendar.getInstance();
cal2.set(year2, month2, day22);
if (cal1.getTime().compareTo(cal2.getTime()) > 0) {
isEarly = 1;
} else if (cal1.getTime().compareTo(cal2.getTime()) == 0) {
isEarly = 0;
}
}
return isEarly;
}
/**
* <PRE>
*
* 比较两个日期
*
* </PRE>
*
* @param day1
* String
* @param day2
* String
* @param strFormat
* String
* @throws java.text.ParseException
* 异常
* @return int day1>day2返回1,day1<day2返回-1,day1=day2返回0
*/
public static int compareDate(String day1, String day2, String strFormat)
throws ParseException {
Date date1 = getFormatDate(day1, DATE_FORMAT_THREE);
Date date2 = getFormatDate(day2, DATE_FORMAT_THREE);
return date1.compareTo(date2);
}
/**
* 日期追加的计算
*
* @param strDate
* 原日期(YYYYMMDD)
* @param nAddNum
* 追加的年月日的大小
* @param nType
* 追加的类型
*
* @return 追加后的新日期(YYYYMMDD)
*/
public static String addDate(String strDate, int nAddNum, int nType) {
int nYear = Integer.parseInt(strDate.substring(0, 4));
int nMonth = Integer.parseInt(strDate.substring(4, 6)) - 1;
int nDay = Integer.parseInt(strDate.substring(6));
GregorianCalendar objCal = new GregorianCalendar();
objCal.set(nYear, nMonth, nDay);
switch (nType) {
case 1: {
objCal.add(GregorianCalendar.YEAR, nAddNum);
break;
}
case 2: {
objCal.add(GregorianCalendar.MONTH, nAddNum);
break;
}
case 3: {
objCal.add(GregorianCalendar.DATE, nAddNum);
break;
}
default: {
break;
}
}
return dateToString(objCal.getTime(), DATE_FORMAT_THREE);
}
/**
* 日期追加的计算
*
* @param date
* 原日期
* @param nAddNum
* 追加的年月日的大小
* @param nType
* 追加的类型
*
* @return 追加后的新日期
*/
public static Date addDate(Date date, int nAddNum, int nType) {
GregorianCalendar objCal = new GregorianCalendar();
objCal.setTime(date);
switch (nType) {
case 1: {
objCal.add(GregorianCalendar.YEAR, nAddNum);
break;
}
case 2: {
objCal.add(GregorianCalendar.MONTH, nAddNum);
break;
}
case 3: {
objCal.add(GregorianCalendar.DATE, nAddNum);
break;
}
}
return objCal.getTime();
}
/**
* 返回日期比较的中文格式
*
* @param date1
* 日期一
* @param date2
* 日期二
* @return ××年××月××天
*/
public static String toChineseDiffDate(String date1, String date2) {
int diffMonth = 0;
int diffDay = 0;
int year = Integer.parseInt(date1.substring(0, 4));
int year1 = Integer.parseInt(date2.substring(0, 4));
int month = Integer.parseInt(date1.substring(4, 6)) - 1;
int month1 = Integer.parseInt(date2.substring(4, 6)) - 1;
int day = Integer.parseInt(date1.substring(6, 8));
int day1 = Integer.parseInt(date2.substring(6, 8));
Calendar startCalendar = Calendar.getInstance();
startCalendar.set(year, month, day, 0, 0, 0);
Calendar endCalendar = Calendar.getInstance();
endCalendar.set(year1, month1, day1, 0, 0, 0);
Date enddate = endCalendar.getTime();
Date startdate = startCalendar.getTime();
int monthDays = startCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
while (startdate.before(enddate)) {
diffDay++;
startCalendar.add(Calendar.DATE, 1);
startdate = startCalendar.getTime();
if (diffDay == monthDays) {
diffMonth++;
diffDay = 0;
monthDays = startCalendar
.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}
String strdate = "";
if (diffMonth / 12 > 0) {
strdate = strdate + diffMonth / 12 + "年";
}
if (diffMonth % 12 > 0) {
strdate = strdate + diffMonth % 12 + "月";
}
if (strdate.equals("") || diffDay > 0) {
strdate = strdate + diffDay + "日";
}
return strdate;
}
/**
*
* @param date
* 日期
* @param strLangFlag
* 语言标志
* @return 中文化的星期X
*/
public static String getCnEnDayOfWeek(Date date, String strLangFlag) {
String strDayOfWeek = "星期";
String strEnDayOfWeek = "";
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == 2) {
strDayOfWeek += "一";
strEnDayOfWeek += "Monday";
} else if (dayOfWeek == 3) {
strDayOfWeek += "二";
strEnDayOfWeek += "Tuesday";
} else if (dayOfWeek == 4) {
strDayOfWeek += "三";
strEnDayOfWeek += "Wednesday";
} else if (dayOfWeek == 5) {
strDayOfWeek += "四";
strEnDayOfWeek += "Thursday";
} else if (dayOfWeek == 6) {
strDayOfWeek += "五";
strEnDayOfWeek += "Friday";
} else if (dayOfWeek == 7) {
strDayOfWeek += "六";
strEnDayOfWeek += "Saturday";
} else {
strDayOfWeek += "日";
strEnDayOfWeek += "Sunday";
}
if (StringUtil.isEmpty(strLangFlag)) {
return strDayOfWeek;
}
return strEnDayOfWeek;
}
/**
*
* @param date
* 日期
* @param strLangFlag
* 日期
* @return 英文日期
*/
public static String getCurrentDayOfWeek(Date date, String strLangFlag) {
SimpleDateFormat sdf = null;
if (StringUtil.isEmpty(strLangFlag)) {
sdf = new SimpleDateFormat(DATE_FORMAT_FORTY, Locale.CHINESE);
} else {
sdf = new SimpleDateFormat(DATE_FORMAT_THIRTY, Locale.ENGLISH);
}
String strDate = sdf.format(Calendar.getInstance().getTime());
return strDate;
}
/**
*
* @param date
* 日期
* @throws java.text.ParseException
* 异常
* @return 今年最后一天 yyyy-MM-dd
*/
public static Date getNowYearLastDay(Date date) throws ParseException {
String lastDay = new SimpleDateFormat(DATE_FORMAT_SIX).format(date)
+ "-12-31";
return new SimpleDateFormat(DATE_FORMAT_ONE).parse(lastDay);
}
/**
*
* @param sd1
* 日期
* @param ed2
* 日期
* @return 日期相隔天数
*/
public static int diffDay(Date sd1, Date ed2) {
Calendar d1 = Calendar.getInstance();
Calendar d2 = Calendar.getInstance();
d1.setFirstDayOfWeek(Calendar.SUNDAY);
d2.setFirstDayOfWeek(Calendar.SUNDAY);
d1.setTime(sd1);
d2.setTime(ed2);
if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
Calendar swap;
swap = d1;
d1 = d2;
d2 = swap;
}
int days = d2.get(Calendar.DAY_OF_YEAR)
- d1.get(Calendar.DAY_OF_YEAR);
int y2 = d2.get(Calendar.YEAR);
if (d1.get(Calendar.YEAR) != y2) {
d1 = (Calendar) d1.clone();
do {
days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
d1.add(Calendar.YEAR, 1);
} while (d1.get(Calendar.YEAR) != y2);
}
return days;
}
/**
*
* @param date
* 日期
* @param date1
* 日期
* @param n
* 倍数
* @return 两个日期之间间隔天数是否是n的倍数
*/
public static boolean isDayMultN(Date date, Date date1, int n) {
return (diffDay(date, date1) % n) == 0;
}
/**
*
* @param date
* 日期
* @param date1
* 日期
* @return 日期相隔周数
*/
public static int diffWeek(Date date, Date date1) {
return (int) ((getNextMonday(date).getTime() - getNextMonday(date1)
.getTime()) / (1000 * 60 * 60 * 3600 * 7));
}
/**
*
* @param date
* 日期
* @param date1
* 日期
* @param n
* 倍数
* @return 两个日期之间间隔周数是否是n的倍数
*/
public static boolean isWeekMultN(Date date, Date date1, int n) {
return (diffWeek(date, date1) % n) == 0;
}
/**
* @param date
* 日期
* @return 获得周一的日期
*/
public static Date getMonday(Date date) {
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return c.getTime();
}
/**
* @param date
* 日期
* @return 获得周六的日期
*/
public static Date getSaturday(Date date) {
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
return c.getTime();
}
/**
* @param date
* 日期
* @return 获得某日为该周第几天
*/
public static int getDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK);
}
/**
* @param date
* 日期
* @return 获得当月第一天
*/
public static Date getFirstDayOfCurrMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.setTime(date);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
calendar.add(Calendar.DATE, 1 - dayOfMonth);
return calendar.getTime();
}
/**
* 取得某天是一年中的多少周
*
* @param date
* date
* @return 某天是一年中的多少周
*/
public static int getWeekOfYear(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int currMonth = c.get(Calendar.MONTH);
if (currMonth == 11 && c.get(Calendar.WEEK_OF_YEAR) == 1) {
// 取上周周一
c.add(Calendar.DATE, -7);
return c.get(Calendar.WEEK_OF_YEAR) + 1;
} else {
return c.get(Calendar.WEEK_OF_YEAR);
}
}
/**
* 得到某一年周的总数
*
* @param year
* year
* @return 某一年周的总数
*/
public static int getMaxWeekNumOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
return getWeekOfYear(c.getTime());
}
/**
* @param date
* 日期
* @return 该月第几周
*/
public static int getWeekOfMonth(Date date) {
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
return c.get(Calendar.WEEK_OF_MONTH);
}
/**
* 获得日期的下一个星期一的日期
*
* @param day
* day
* @return 日期的下一个星期一的日期
*/
public static Date getNextMonday(Date day) {
Calendar date = Calendar.getInstance();
date.setFirstDayOfWeek(Calendar.SUNDAY);
date.setTime(day);
Calendar result = null;
result = date;
do {
result = (Calendar) result.clone();
result.add(Calendar.DATE, 1);
} while (result.get(Calendar.DAY_OF_WEEK) != 2);
return result.getTime();
}
/**
* 获得本周第一天的日期(星期天为第一天)
*
* @param day
* day
* @return 本周第一天的日期(星期天为第一天)
*/
public static Date getFirstDayOfWeek(Date day) {
Calendar date = Calendar.getInstance();
date.setFirstDayOfWeek(Calendar.SUNDAY);
date.setTime(day);
date.set(Calendar.DAY_OF_WEEK, date.getFirstDayOfWeek());
return date.getTime();
}
/**
* 获得日期包含月份的最大日期
*
* @param dateString
* 日期字符串 "2000-01-01"
* @param applyPattern
* 日期的模式 "yyyy-MM-dd"
* @throws java.text.ParseException
* 异常
* @return 最大日期
*/
public static int getMaxDayOfMonth(String dateString, String applyPattern)
throws ParseException {
int d = 0;
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.SUNDAY);
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern("yyyy-MM-dd");
c.setTime(sdf.parse(dateString));
d = c.getActualMaximum(Calendar.DAY_OF_MONTH);
return d;
}
/**
* 获得当前年的前n年和后n年
*
*/
public static String[] getYearArr(int n) {
String years[] = new String[2 * n + 1];
// 当前年
String currentYear = dateToString(Calendar.getInstance().getTime(),
"yyyy");
for (int i = n; i >= 0; i--) {
// 前n年的选项
years[n - i] = "" + (Integer.parseInt(currentYear) - i);
// 后n年的选项
years[n + i] = "" + (Integer.parseInt(currentYear) + i);
}
return years;
}
/**
* <PRE>
*
* DateFormat转化.
*
* </PRE>
*
* @param value
* String
* @param fromFormatStr
* String
* @param toFormatStr
* String
*
*
* @return 转化后的Date
*
* @throws java.text.ParseException
* ParseException
*/
public static String parseDateFormat(String value, String fromFormatStr,
String toFormatStr) throws ParseException {
if (value == null || value.equals("")) {
return null;
}
DateFormat formFormat = new SimpleDateFormat(fromFormatStr);
formFormat.setLenient(false);
DateFormat toFormat = new SimpleDateFormat(toFormatStr);
toFormat.setLenient(false);
return toFormat.format(formFormat.parse(value));
}
/**
* @param date
* 日期
* @return 获取时间戳
*/
public static long getTimes(Date date) {
long stamp = 0L;
// 返回的是年月日时分秒
Date date1 = DateUtils.formatDate(date, DateUtils.DATE_FORMAT_FIVE);
stamp = date1.getTime() / 1000;
return stamp;
}
/**
* 根据10位时间戳获取格式化后时间
*
* @param time
* 10位时间戳
* @param format
* 格式化
* @return
*/
public static String formatTimes(Long time, String format) {
if (time == null || Long.valueOf(0) == time)
return "";
if (StringUtil.isEmpty(format)) {
format = DateUtils.DATE_FORMAT_FIVE;
}
time = time * 1000;
return dateToString(new Date(time), format);
}
/**
* 两个小时前的时间戳
*
* @param date
* @return
*/
public static long twoHourAgo(Date date) {
long time = getTimes(date) - 7200;
return time;
}
/**
* 获取一天后的时间
*
* @param n
* @return Date
*/
public static Date getHoursLater(int n) {
// 获得两小时之后的时间
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.HOUR, n);
Date date = calendar.getTime();
return date;
}
/**
* String转时间戳
*
* @param strDate
* @param format
* @return
*/
public static long StringToTimestamp(String strDate, String format) {
Date date;
long time = 0L;
date = getFormatDate(strDate, format);
time = getTimes(date);
return time;
}
/**
* 获取某月的第一天
*
* @param
* @return Date
*/
public static Date getFisrtDay(Date date) {
// 获得两小时之后的时间
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
date = calendar.getTime();
String strDate = dateToString(date, DATE_FORMAT_ONE) + START_TIME;
date = getFormatDate(strDate, DATE_FORMAT_FIVE);
return date;
}
/**
* 获取某月的第一天
*
* @param
* @return Date
*/
public static Date getLastDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 0);
String strDate = dateToString(calendar.getTime(), DATE_FORMAT_ONE)
+ END_TIME;
date = getFormatDate(strDate, DATE_FORMAT_FIVE);
return date;
}
/**
* 获取当前日期的前n天
*
* @param day
* @return Date
*/
public static Date getBeforeDay(int day) {
Date date = null;
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE, -(day));
String strDate = dateToString(calendar.getTime(), DATE_FORMAT_ONE)
+ START_TIME;
date = getFormatDate(strDate, DATE_FORMAT_FIVE);
return date;
}
/**
* 根据10位时间戳获取格式化后时间
*
* @param timestamp
* @param format 10位时间戳
* @return
*/
public static Date timestampToDate(String timestamp, String format) {
String strDate = "";
Date date = null;
if (!StringUtil.isEmpty(timestamp)) {
long time = Long.parseLong(timestamp);
strDate = dateToString(new Date(time), format);
date = getFormatDate(strDate, format);
}
return date;
}
/**
* 获取星期
*
* @param
* @return
*/
public static String week() {
Date date = new Date();
String[] weekDays = { "0", "1", "2", "3", "4", "5", "6" };
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 当前的开始时间
*
* @param times
* 10位时间戳
* @return
*/
@SuppressWarnings("deprecation")
public static int seconds(String times) {
int time = DateUtils.timestampToDate(times, DateUtils.DATE_FORMAT_FIVE)
.getHours()
* 3600
+ DateUtils.timestampToDate(times, DateUtils.DATE_FORMAT_FIVE)
.getMinutes() * 60;
return time;
}
/**
* 获取n小时后的时间
*
* @param time
* @param hours
* @param format
* @return Date
*/
public static Date getAfferHours(String time, int hours, String format) {
Date AfferDate = null;
// 时间戳转string
String strDate = "";
// 获得两小时之后的时间
Calendar calendar = Calendar.getInstance();
calendar.setTime(timestampToDate(time, format));
calendar.add(Calendar.HOUR, hours);
Date date = calendar.getTime();
strDate = dateToString(date, format);
AfferDate = getFormatDate(strDate, format);
return AfferDate;
}
/**
* 获取n小时后的时间
*
* @param time
* @param hours
* @param format
* @return Date
*/
public static Date getSendMessageTimes(String time, String hours,
String format) {
Date date = null;
// 时间戳转string
long timestamp = (long) (Double.valueOf(time) + Double.valueOf(hours) * 3600);
date = timestampToDate(String.valueOf(timestamp), format);
return date;
}
/**
* 获取十分钟前的时间
*
* @param time
* @param hours
* @param format
* @return Date
*/
public static Date getMinutesBefore(String time, String hours, String format) {
Date date = null;
// 时间戳转string
long timestamp = (long) (Double.valueOf(time) - Double.valueOf(hours) * 60);
date = timestampToDate(String.valueOf(timestamp), format);
return date;
}
/**
* 获取当前时间分秒的时间戳<br>
*
* @return seconds
*/
public static int getSeconds() {
Date date = new Date();
@SuppressWarnings("deprecation")
int seconds = date.getHours() * 3600 + date.getMinutes() * 60;
return seconds;
}
/**
* 获取date的前(intervalHour)个小时时间
*
* @param date
* @param intervalHour
* @return
*/
public static Date getIntervalTime(Date date, int intervalHour) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)
- intervalHour);
return DateUtils.formatDate(calendar.getTime(),
DateUtils.DATE_FORMAT_FIVE);
}
/**
* 获取指定时间的n分钟后的时间
*
* @param dateline
* @param n
* @return Date
*/
public static Date getTimesLater(Date dateline, int n) {
// 获得两小时之后的时间
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateline);
calendar.add(Calendar.MINUTE, n);
Date date = calendar.getTime();
return date;
}
/**
* 时间比较24小时内
* @param orderTime
* @param date
* @return
*/
public static boolean timeDiff(Date orderTime,Date date){
if(date.getTime()-orderTime.getTime()<86400000){
return true;
}else{
return false;
}
}
/**
* 获取n个月后的时间
*
* @param day
* @return Date
*/
public static Date getMonthLater(int month) {
Date date = null;
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, month);
String strDate = dateToString(calendar.getTime(), DATE_FORMAT_ONE)
+ START_TIME;
date = getFormatDate(strDate, DATE_FORMAT_FIVE);
return date;
}
public static void main(String[] args) {
}
}