import org.apache.commons.lang3.time.DateUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期处理的工具类
*
*/
public class DateUtil extends DateUtils {
/**
* 日期的格式
*/
public static final String DATE_FORMAT = "yyyy-MM-dd";
/**
* 日期的格式 年 月
*/
public static final String MONTH_FORMAT = "yyyy年MM月";
/**
* 小时:分秒的格式
*/
public static final String HHMMSS_FORMAT = "HH:mm:ss";
public static final DateFormat DAY_FORMAT=new SimpleDateFormat("yyyy年MM月dd日");
/**
* 小时:分的格式
*/
public static final String HHMM_FORMAT = "HH:mm";
private static final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
public final static DateFormat yearFormat = new SimpleDateFormat("yyyy");
public final static DateFormat monthFormat = new SimpleDateFormat("MM");
public final static DateFormat dayFormat = new SimpleDateFormat("dd");
public final static DateFormat yearmonthFormat = new SimpleDateFormat("yyyy-MM");
public final static DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
public final static DateFormat dateFormat2 = new SimpleDateFormat("yyyyMMdd");
public final static DateFormat dateFormat3 = new SimpleDateFormat("yyyy.MM.dd");
public final static DateFormat dateFormat4 = new SimpleDateFormat("yy/MM/dd");
public final static DateFormat moniteFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
public final static DateFormat cnTimeFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
public final static DateFormat secondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public final static DateFormat secondSlashFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public final static DateFormat timeFormat = new SimpleDateFormat("HH:mm");
public final static DateFormat secondFormat_format = new SimpleDateFormat("yyyyMMddHHmmss");
public final static DateFormat yearMonthFormatCn = new SimpleDateFormat(MONTH_FORMAT);
public final static DateFormat hourMoniteSecondFormat = new SimpleDateFormat("HH:mm:ss");
/**
* 获取13位时间戳(1401433624722)
*
* @return
*/
public static final String getDateTime() {
Date date = new Date();
long time = date.getTime();
return String.valueOf(time);
}
public static final String getTimeStamp(Date date) {
long time = date.getTime();
return String.valueOf(time);
}
public static int daysBetween(Date smdate, Date bdate) throws ParseException {
// smdate = secondFormat.parse(secondFormat.format(smdate));
// bdate = secondFormat.parse(secondFormat.format(bdate));
Calendar cal = Calendar.getInstance();
cal.setTime(smdate);
long time1 = cal.getTimeInMillis();
cal.setTime(bdate);
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 将日期字符串解析成"yyyy-MM-dd"格式的Date对象
*
* @param dateTime
* @return
*/
public static synchronized Date parseDate(String dateTime) {
try {
return dateFormat.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 将日期字符串解析成"yyyyMMddHHmmss"格式的Date对象
*
* @param dateTime
* @return
*/
public static synchronized Date parseDate2(String dateTime) {
try {
return secondFormat_format.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 将日期字符串解析成"yyyy-MM-dd HH:mm:ss"格式的Date对象
*
* @param dateTime
* @return
*/
public static synchronized Date parseDateTime(String dateTime) {
try {
return secondFormat.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String formatYYYYMMDDChinese(Date date){
if (date == null)
return null;
return DAY_FORMAT.format(date);
}
/**
* 将日期字符串解析成指定格式的Date对象
*
* @param dateTime
* @param format
* @return
*/
public static synchronized Date parse(String dateTime, String format) {
if (StringUtil.isBlank(dateTime))
return null;
DateFormat dateFormat = new SimpleDateFormat(format);
try {
return dateFormat.parse(dateTime);
} catch (ParseException e) {
throw new RuntimeException("format date error!", e);
}
}
/**
* 将日期类解析成"yyyy-MM-dd HH:mm:ss"格式的日期字符串
*
* @param date
* @return
*/
public static synchronized String formatYYYYMMDDHHMMSS(Date date) {
if (date == null)
return null;
return secondFormat.format(date);
}
/**
* 将日期类解析成"yyyy-MM-dd HH:mm:ss"格式的日期字符串
*
* @param date
* @return
*/
public static synchronized String formatYYYYMMDDHHMM(Date date) {
if (date == null)
return null;
return moniteFormat.format(date);
}
/**
* 将日期类解析成"yyyyMMddHHmmss"格式的日期字符串
*
* @param date
* @return
*/
public static synchronized String formatYYYYMMDDHHMMSS_2(Date date) {
if (date == null)
return null;
return secondFormat_format.format(date);
}
/**
* 将日期类解析成"yyyy-MM-dd"格式的日期字符串
*
* @param date
* @return
*/
public static synchronized String formatYYYYMMDD(Date date) {
if (date == null)
return null;
return dateFormat.format(date);
}
/**
* 将日期类解析成"yyyyMMdd"格式的日期字符串
*
* @param date
* @return
*/
public static synchronized String formatYYYYMMDD2(Date date) {
if (date == null)
return null;
return dateFormat2.format(date);
}
/**
* 将日期类解析成"yyyy.MM.dd"格式的日期字符串
*
* @param date
* @return
*/
public static synchronized String formatYYYYMMDD3(Date date) {
if (date == null)
return null;
return dateFormat3.format(date);
}
/**
* 将日期类解析成"yy/MM/dd"格式的日期字符串
*
* @param date
* @return
*/
public static synchronized String formatYYMMDD4(Date date) {
if (date == null)
return null;
return dateFormat4.format(date);
}
/**
* @Description:
* @Author: wenyidao
* @Date: 2018/9/29
*/
public static synchronized String formatYYYY(Date date) {
if (date == null) {
return null;
}
return yearFormat.format(date);
}
/**
* 将日期类解析成"yyyy-MM"格式的日期字符串
*
* @param date
* @return
*/
public static synchronized String formatYYYYMM(Date date) {
if (date == null)
return null;
return yearmonthFormat.format(date);
}
// /**
// * 将日期类解析成指定格式的日期字符串
// *
// * @param date
// * @param format
// * @return
// */
// public static synchronized String formatDateTime(Date date, String
// format) {
// if (date == null)
// return null;
// DateFormat dateFormat = new SimpleDateFormat(format);
// return dateFormat.format(date);
// }
/**
* 返回当月的星期几
*
* @param date
* @return
*/
public static synchronized String formatWeekInMonth(Date date) {
if (date == null)
return null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return dayNames[cal.get(Calendar.DAY_OF_WEEK) - 1];
}
/**
* 返回时间差(秒)
*
* @param old
* @return
*/
public static synchronized int getDiffeSeconds(Date old) {
if (old == null)
return 0;
return getDiffeSeconds(null, old);
}
/**
* 返回时间差(秒)
*
* @param now
* @param old
* @return
*/
public static synchronized int getDiffeSeconds(Date now, Date old) {
if (old == null)
return 0;
if (now == null)
now = new Date();
return (int) ((now.getTime() - old.getTime()) / 1000);
}
/**
* 返回时间差(分钟)
*
* @param old
* @return
*/
public static synchronized int getDiffeMinute(Date old) {
if (old == null)
return 0;
return getDiffeMinute(null, old);
}
/**
* 返回时间差(分钟)
*
* @param now
* @param old
* @return
*/
public static synchronized int getDiffeMinute(Date now, Date old) {
if (old == null)
return 0;
if (now == null)
now = new Date();
return (int) ((now.getTime() - old.getTime()) / (1000 * 60));
}
/**
* 返回时间差(小时)
*
* @param old
* @return
*/
public static synchronized int getDiffeHour(Date old) {
if (old == null)
return 0;
return getDiffeHour(null, old);
}
/**
* 返回时间差(小时)
*
* @param now
* @param old
* @return
*/
public static synchronized int getDiffeHour(Date now, Date old) {
if (old == null)
return 0;
if (now == null)
now = new Date();
return (int) ((now.getTime() - old.getTime()) / (1000 * 60 * 60));
}
/**
* 返回时间差(天)
*
* @param old
* @return
*/
public static synchronized int getDiffeDay(Date old) {
if (old == null)
return 0;
return getDiffeDay(null, old);
}
/**
* 返回时间差(天>小时>分)
*
* @param now
* @param old
* @return
*/
public static synchronized String getDiffeDayHourMinute(Date old) {
int m = (int) (new Date().getTime() - old.getTime()) / (1000 * 60);
int day = m / (24 * 60);
m %= (24 * 60);
int h = m / (60);
m %= 60;
if (day != 0)
return day + "天";
if (h != 0)
return h + "小时";
return m + "分钟";
}
/**
* 返回离当前时间时间差()
*
* @param now
* @param old
* @return
*/
public static synchronized int getDiffeDay(Date now, Date old) {
if (old == null)
return 0;
if (now == null)
now = new Date();
return BigDecimal.valueOf(((now.getTime() - old.getTime()) / (1000 * 60 * 60 * 24))).setScale(0, RoundingMode.CEILING) .intValue();
}
/**
* @Description 支付用
* @param date
* @return
* @author zhaoyonglian
* @date 2015年8月10日 下午4:47:49
*/
public static synchronized String formatTime(Date date) {
return secondFormat_format.format(date);
}
/**
* @Description 支付用
* @param date
* @return
* @author zhaoyonglian
* @date 2015年8月10日 下午4:47:49
*/
public static synchronized String formatDate(Date date) {
return secondFormat.format(date);
}
/**
* 格式化成中文时间
* @param date
* @return
*/
public static synchronized String formatDateToCN(Date date) {
return cnTimeFormat.format(date);
}
/**
* 返回当月的第一天
*
* @return
*/
public static Date firstDayOfMonth() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* 按照"yyyyMMddHHmmss"格式化时间
*
* @param date
* @return
*/
public static String formatYMDHMS(Date date) {
return secondFormat_format.format(date);
}
/**
* 返回离当前时间时间差()
*
* @param now
* @param old
* @return
*/
public static Long getDiffeDayNow(Date old) {
if (old == null)
return null;
Long result = (Long) ((System.currentTimeMillis() - old.getTime()) / (1000 * 60 * 24));//返回分钟
return result;
}
/**
* 返回离当前时间时间差()
*
* @param now
* @param old
* @return
*/
public static Long getDiffeDayNowInt(Date old) {
Long result = (Long) (old.getTime() / (1000 * 60 * 60 * 24) - System.currentTimeMillis()
/ (1000 * 60 * 60 * 24));
if (old.getTime() - System.currentTimeMillis() >= 0) {
if (result == 0) {
return 1l;
}
}
return result + 1l;
}
public static Date addDay(String s, int n) {
try {
Calendar cd = Calendar.getInstance();
cd.setTime(dateFormat.parse(s));
// 增加一天
cd.add(Calendar.DATE, n);
return cd.getTime();
} catch (Exception e) {
return null;
}
}
public static Date addDay(Date date, int n) {
try {
Calendar cd = Calendar.getInstance();
cd.setTime(date);
// 增加一天
cd.add(Calendar.DATE, n);
return cd.getTime();
} catch (Exception e) {
return null;
}
}
public static Date addMonth(String s, int n) {
try {
Calendar cd = Calendar.getInstance();
cd.setTime(dateFormat.parse(s));
//增加一个月
cd.add(Calendar.MONTH, n);
return cd.getTime();
} catch (Exception e) {
return null;
}
}
public static Date addMonth(Date date, int n) {
try {
Calendar cd = Calendar.getInstance();
cd.setTime(date);
//增加一个月
cd.add(Calendar.MONTH, n);
return cd.getTime();
} catch (Exception e) {
return null;
}
}
public static Date addYear(String s, int n) {
try {
Calendar cd = Calendar.getInstance();
cd.setTime(dateFormat.parse(s));
//增加一年
cd.add(Calendar.YEAR, n);
return cd.getTime();
} catch (Exception e) {
return null;
}
}
public static Date addYear(Date date, int n) {
try {
Calendar cd = Calendar.getInstance();
cd.setTime(date);
//增加一年
cd.add(Calendar.YEAR, n);
return cd.getTime();
} catch (Exception e) {
return null;
}
}
public static Date addDayForDate(Date s, int n) {
try {
Calendar cd = Calendar.getInstance();
String format = dateFormat.format(s);
cd.setTime(dateFormat.parse(format));
// 增加一天
cd.add(Calendar.DATE, n);
return cd.getTime();
} catch (Exception e) {
return null;
}
}
/**
* 当前时间添加指定属性的数量
* @param field
* @param amount
* @return
*/
public static Date getNowDateAddField(int field , int amount) {
Calendar cd = Calendar.getInstance();
cd.add(field,amount);
return cd.getTime();
}
/**
* 当前时间添加指定属性的数量
* @param date
* @param amount
* @return
*/
public static Date operateDateByField(Date date , int field , int amount) {
Calendar cd = Calendar.getInstance();
cd.setTime(date);
cd.add(field,amount);
return cd.getTime();
}
public static Date getNowDate() {
return new Date();
}
/**
* 解析成"yyyy/MM/dd HH:mm:ss"格式的TimeStamp
*
* @param date
* @return
*/
public static synchronized Timestamp timeStampYYYYMMDDHHMMSS(String dateTimeStr) {
if (dateTimeStr == null || dateTimeStr == "" || dateTimeStr == "null"){
return null;
}
Date dateTime = null;
try {
dateTime = secondSlashFormat.parse(dateTimeStr);
} catch (ParseException e) {
e.printStackTrace();
}
Timestamp ts = new Timestamp(dateTime.getTime());
return ts;
}
public static Date parseDate(Long timestamp) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = format.format(timestamp);
Date date= null;
try {
date=format.parse(d);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 解析成"yyyy/MM/dd HH:mm:ss"格式的String
*
* @param date
* @return
*/
public static synchronized String formatYYYYMMDDHHMMSS3(Date dateTime) {
if (dateTime == null){
return null;
}
return secondSlashFormat.format(dateTime);
}
/**
* 返回"HH:mm"格式String
*
* @param dateTime
* @return
*/
public static synchronized String formatHHMM(Date dateTime) {
if (dateTime == null){
return null;
}
return timeFormat.format(dateTime);
}
/**
* 时间戳转日期字符串
*
* @param time
* @return
*/
public static String timeStamp2yyyyMMdd(String time) {
Long timeLong = Long.parseLong(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//要转换的时间格式
Date date;
try {
date = sdf.parse(sdf.format(timeLong));
return sdf.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args){
Long sLong = 445555555L;
Date parseDate = parseDate(sLong);
System.out.println(parseDate);
}
}