import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期工具类
*
* @author PanJun
*
*/
public final class DateUtil {
private static final String YYYY_MM_DD = "yyyy-MM-dd";
private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static final ThreadLocal<SimpleDateFormat> dateFmtInstance = new ThreadLocal<SimpleDateFormat>() {
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyyMMddHHmmss");
};
};
private static ThreadLocal<Calendar> calInstance = new ThreadLocal<Calendar>() {
protected Calendar initialValue() {
return Calendar.getInstance();
}
};
/**
* 把日期加天数
*
* @param date
* @param day
* @return
*/
public static Date addDay(Date date, int day) {
if (date == null)
return null;
Calendar calendar = calInstance.get();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, day);
return calendar.getTime();
}
/**
* 把日期减天数
*
* @param date
* @param day
* @return
*/
public static Date rollDay(Date date, int day) {
if (date == null)
return null;
Calendar calendar = calInstance.get();
calendar.setTime(date);
calendar.roll(Calendar.DAY_OF_MONTH, day);
return calendar.getTime();
}
/**
* 获取当前日期月的最后一天
*
* @param year
* @param math
* @return
*/
public static Integer getLastDay(Date date) {
Integer year = DateUtil.getYear(date);
Integer month = DateUtil.getMonth(date);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
// 不加下面2行,就是取当前时间前一个月的第一天及最后一天
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
String day_last = df.format(cal.getTime());
StringBuffer endStr = new StringBuffer().append(day_last).append(" 23:59:59");
Date date2 = DateUtil.strToDate(endStr.toString());
return DateUtil.getDayDate(date2);
}
/**
* 把日期加天数,返回结果
*
* @param date
* @param day
* @return
*/
public static Long addDay(Long date, int day) {
if (date == null)
return null;
Date realDate = addDay(toRealDate(date), day);
return toLongDate(realDate);
}
/**
* 把日期加上指定分钟数,返回结果
*
* @param date
* @param minutes
* @return
*/
public static Date addMin(Date date, int minutes) {
if (date == null)
return null;
Calendar calendar = calInstance.get();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, minutes);
return calendar.getTime();
}
/**
* 把日期加上指定分钟数,返回结果
*
* @param date
* @param hours
* @return
*/
public static Date addHour(Date date, int hours) {
if (date == null)
return null;
Calendar calendar = calInstance.get();
calendar.setTime(date);
calendar.add(Calendar.HOUR, hours);
return calendar.getTime();
}
/**
* 请参看Calender.get(field)函数
*
* @param date
* @param field
* @return
*/
public static int get(Date date, int field) {
if (date == null) {
return -1;
}
Calendar cal = calInstance.get();
cal.setTime(date);
return cal.get(field);
}
/**
* 请参看Calender.get(field)函数
*
* @param date
* @param field
* @return
*/
public static int get(Long date, int field) {
return get(toRealDate(date), field);
}
/**
* 把长整形日期转换成真实日期
*
* @param date
* @return
*/
public static Date toRealDate(Long date) {
if (date == null)
return null;
Calendar calendar = calInstance.get();
int ss = (int) (date - (date / 100) * 100);
calendar.set(Calendar.SECOND, ss);
int mi = (int) ((date - (date / 10000) * 10000) / 100);
calendar.set(Calendar.MINUTE, mi);
int hh = (int) ((date - (date / 1000000) * 1000000) / 10000);
calendar.set(Calendar.HOUR_OF_DAY, hh);
int dd = (int) ((date - (date / 100000000) * 100000000) / 1000000);
calendar.set(Calendar.DAY_OF_MONTH, dd);
int yy = (int) (date / 10000000000l);
calendar.set(Calendar.YEAR, yy);
int noYear = (int) (date - yy * 10000000000l);
int mm = noYear / 100000000;
calendar.set(Calendar.MONTH, mm - 1);
return calendar.getTime();
}
/**
* 把日期转换成yyyyMMddHH格式
*
* @param date
* @return
*/
public static Integer toYmdh(Date date) {
if (date != null) {
return Integer.parseInt(new SimpleDateFormat("yyyyMMddHH").format(date));
}
else {
return null;
}
}
/**
* 把日期转换成yyyyMMdd格式
*
* @param date
* @return
*/
public static Integer toYmd(Date date) {
if (date != null) {
return Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(date));
}
else {
return null;
}
}
/**
* Date类型转换成Long型日期
*
* @param date
* @return
*/
public static Long toLongDate(Date date) {
if (date == null)
return null;
return Long.valueOf(dateFmtInstance.get().format(date));
}
/**
* 把日期加月份
*
* @param date
* @param month
* @return
*/
public static Date addMonth(Date date, int month) {
if (date == null)
return null;
Calendar calendar = calInstance.get();
calendar.setTime(date);
calendar.add(Calendar.MONTH, month);
return calendar.getTime();
}
public static Date setDayOfMonth(Date date, int value) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_MONTH, value);
return c.getTime();
}
/**
* 把日期加月份
*
* @param date
* @param month
* @return
*/
public static Long addMonth(Long date, int month) {
if (date == null)
return null;
Date realDate = addMonth(toRealDate(date), month);
return toLongDate(realDate);
}
/**
* 把日期的时分秒去除只留年月日
*
* @param date
* @return 只留年月日的日期
*/
public static Date clearTime(Date date) {
if (date == null)
return null;
Calendar calendar = calInstance.get();
calendar.setTime(date);
int y = calendar.get(Calendar.YEAR);
int m = calendar.get(Calendar.MONTH);
int d = calendar.get(Calendar.DAY_OF_MONTH);
calendar.clear();
calendar.set(Calendar.YEAR, y);
calendar.set(Calendar.MONTH, m);
calendar.set(Calendar.DAY_OF_MONTH, d);
return calendar.getTime();
}
/**
* 设置日期格式的时间部分包含:时、分、秒;如果某部分为负数,不修改此部分
*
* @param date
* @param hour
* @param minute
* @param second
* @return
*/
public static Date setTime(Date date, int hour, int minute, int second) {
Calendar cal = calInstance.get();
cal.setTime(date);
if (hour >= 0) {
cal.set(Calendar.HOUR_OF_DAY, hour);
}
if (minute >= 0) {
cal.set(Calendar.MINUTE, minute);
}
if (second >= 0) {
cal.set(Calendar.SECOND, second);
}
return cal.getTime();
}
/**
* 把日期的时分秒去除只留年月日 20101230115511 == >20101230115511
*
* @param date
* @return 只留年月日的日期
*/
public static Long clearTime(Long date) {
if (date == null)
return null;
return (date / 1000000) * 1000000;
}
/**
* 日期转化为字串
*
* @param date
* @param pattern
* @return
*/
public static String dateToStr(Date date, String pattern) {
if (date == null || pattern == null)
return null;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
/**
* 日期转化为字串
*
* @param date
* @param pattern
* @return
*/
public static String dateToStrFormat(Date date) {
String pattern = "yyyy-MM-dd HH:mm:ss";
if (date == null || pattern == null)
return null;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
/**
* 日期时间转化为字串yyyy-MM-dd HH:mm:SS
*
* @param date
* @param pattern
* @return
*/
public static String dateTimeToStr(Date date) {
return dateToStr(date, YYYY_MM_DD_HH_MM_SS);
}
/**
* 把日期转化成"yyyy-MM-dd"格式的字串
*
* @param date
* @return
*/
public static String dateToStr(Date date) {
return dateToStr(date, YYYY_MM_DD);
}
/**
* 把日期转成日期想要的格式
*
* @param date
* @param pattern
* @return
*/
public static Date dateToDatePattern(Date date, String pattern) {
String dateStr = dateToStr(date, pattern);
return strToDate(dateStr, pattern);
}
/**
* 字串转化成日期
*
* @param date
* @param pattern
* @return
*/
public static Date strToDate(String date, String pattern) {
if (date == null || pattern == null)
return null;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(date);
}
catch (ParseException e) {
throw new RuntimeException(e);
}
}
/**
* 字串转化成日期(不抛异常)
*
* @param date
* @param pattern
* @return
*/
public static Date safeStrToDate(String date, String pattern) {
if (date == null || pattern == null)
return null;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(date);
}
catch (ParseException e) {
return null;
}
}
/**
* 返回两个日期之间的天数差
*
* @param d1
* @param d2
* @return
*/
public static int dateDiff(Date d1, Date d2) {
if (d1 == null || d2 == null)
throw new NullPointerException("dateDiff方法两个参数不能为null!");
d1 = setTime(d1, 0, 0, 0);
d2 = setTime(d2, 0, 0, 0);
Long diff = (d1.getTime() - d2.getTime()) / 1000 / 60 / 60 / 24;
return diff.intValue();
}
/**
* 返回两个Date的月份
*
* @param start
* @param end
* @return
*/
public static int dateMonth(Date start, Date end) {
if (start.after(end)) {
Date t = start;
start = end;
end = t;
}
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(start);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(end);
Calendar temp = Calendar.getInstance();
temp.setTime(end);
temp.add(Calendar.DATE, 1);
int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) == 1)) {
return year * 12 + month + 1;
}
else if ((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) {
return year * 12 + month;
}
else if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1)) {
return year * 12 + month;
}
else {
return (year * 12 + month - 1) < 0 ? 0 : (year * 12 + month);
}
}
/**
* 把毫秒转化成日期
*
* @param dateFormat
* @param millSec
* @return
*/
public static String transferLongToDate(String dateFormat, Long millSec) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Date date = new Date(millSec);
return sdf.format(date);
}
// public static void main(String[] args) {
// DateUtil dateUtil = new DateUtil();
// String string = dateUtil.transferLongToDate("yyyy-MM-dd", 645638400000L);
// System.out.println(string);
// }
/**
* 返回两个日期之间的天数差 结果大于等于0..
*
* @param d1
* @param d2
* @return
*/
public static Long dateDiffLong(Date d1, Date d2) {
if (d1 == null || d2 == null) {
return 0l;
}
d1 = setTime(d1, 0, 0, 0);
d2 = setTime(d2, 0, 0, 0);
Long diff = (d1.getTime() - d2.getTime()) / 1000 / 60 / 60 / 24;
if (diff < 0) {
diff = 0l;
}
return diff;
}
/**
* 返回两个日期之间的天数差
*
* @param d1
* @param d2
* @return
*/
public static int dateDiff(Long d1, Long d2) {
return dateDiff(toRealDate(d1), toRealDate(d2));
}
/**
* 把"yyyy-MM-dd"格式的字串转化成日期
*
* @param date
* @return
*/
public static Date strToDate(String date) {
return strToDate(date, YYYY_MM_DD);
}
/**
* 获取日期是周几
*
* @param dt
* @return
*/
public static int getWeekOfDate(Date dt) {
int[] weekDays = {0, 1, 2, 3, 4, 5, 6};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
public static Boolean isValidDate(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
try {
Date validDate = sdf.parse(date);
String tmp = DateUtil.dateToStr(validDate, "yyyy.MM.dd");
// 格式化之后相等说明日期合法
if (date.equals(tmp)) {
return true;
}
}
catch (ParseException e) {
throw new RuntimeException(e);
}
return false;
}
/**
* 取有效日期
*
* @param year
* 年
* @param month
* 月
* @param day
* 日
* @return 有效日期
*/
public static Date getValidDate(int year, int month, int day) {
int v_year = year;
int v_month = month;
int v_day = day;
String v_date = "";
Date validDate = null;
while (true) {
if (v_date == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
v_date = v_year + "-" + v_month + "-" + v_day;
validDate = sdf.parse(v_date);
String tmp = DateUtil.dateToStr(validDate, "yyyy-M-d");
// System.out.println(v_date +"-" + tmp);
// 格式化之后相等说明日期合法
if (v_date.equals(tmp)) {
return validDate;
}
v_day = v_day - 1;
}
catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
/**
* 获取年
*
* @param date
* @return
*/
public static int getYear(Date date) {
Calendar cal = Calendar.getInstance();// 使用日历类
cal.setTime(date);
int year = cal.get(Calendar.YEAR);// 得到年
return year;
}
/**
* 获取月
*
* @param date
* @return
*/
public static int getMonth(Date date) {
Calendar cal = Calendar.getInstance();// 使用日历类
cal.setTime(date);
int month = cal.get(Calendar.MONTH);// 得到月
return month + 1;
}
/**
* 获取日
*
* @param date
* @return
*/
public static int getDayDate(Date date) {
Calendar cal = Calendar.getInstance();// 使用日历类
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);// 得到日
return day;
}
/**
* 获取还款日期
*
* @param date
* @param payday
* @return
* @see [类、类#方法、类#成员]
*/
public static Date getEndDate(Date date, int payday) {
return DateUtil.getValidDate(DateUtil.getYear(date), DateUtil.getMonth(date), payday);
// System.out.println(DateUtil.dateTimeToStr(date));
}
/**
* 把日期加天数算头算尾
*
* @param date
* @param day
* @return
*/
public static Date addDayWithTail(Date date, int day) {
if (date == null)
return null;
Calendar calendar = calInstance.get();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, day - 1);
return calendar.getTime();
}
/**
* 把日期加月份包含首尾
*
* @param date
* @param month
* @return
*/
public static Date addMonthWithTail(Date date, int month) {
if (date == null)
return null;
Calendar calendar = calInstance.get();
calendar.setTime(date);
calendar.add(Calendar.MONTH, month);
calendar.add(Calendar.DAY_OF_MONTH, -1);
return calendar.getTime();
}
/**
* 将指定日期返回格式* 年*月*日
*
* @param date
* @return
*/
public static String returnDateStr(Date date) {
String dataString = new String();
dataString = DateUtil.getYear(date) + "年" + DateUtil.getMonth(date) + "月" + DateUtil.getDayDate(date) + "日";
return dataString;
}
/**
* 时间戳转换成日期格式字符串
* @param seconds 精确到秒的字符串
* @param formatStr
* @return
*/
public static String timeStampToDate(String seconds,String format) {
if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
return "";
}
if(format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(Long.valueOf(seconds+"000")));
}
/**
* 将秒数转化为几天几小时几分几秒
*
*
* @return String
* @author xuxin
* @addtime Oct 18, 201611:33:07 AM
* @since JDK 1.7
* @see
*/
public static String changeToDateStr(Long seconds) {
if (seconds == null || seconds.longValue() == 0 || seconds.longValue() < 0) {
return null;
}
long v = seconds.longValue();
long day = seconds / (60 * 60 * 24);
long hour = (seconds % (60 * 60 * 24)) / (60 * 60);
long minute = (seconds % (60 * 60)) / 60;
long second = seconds % 60;
String res = "";
if (day > 0) {
res = res + day + "天 ";
}
if (hour > 0) {
res = res + hour + "小时 ";
}
if (minute > 0) {
res = res + minute + "分 ";
}
if (second > 0) {
res = res + second + "秒 ";
}
return res;
}
public static String dateFormat(Date date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = null;
if (date != null) {
dateString = dateFormat.format(date);
}
return dateString;
}
public static SimpleDateFormat dateFormat4yyyyMMddHHmmss1() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
}