import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
/**
* Description:
*
* @author shuaijun
* @Date 2019.12.11
* @since Ver 1.0
*/
public class DateUtils {
public static final Integer ONE_MINTUE = 60;
public static final Integer ONE_HOUR = 60 * 60;
public static final Integer ONE_DAY = 60 * 60 * 24;
public static final Integer ONE_MONTH = 60 * 60 * 24 * 30;
public enum DateFormat {
SHORT("yy-MM-dd"), DATE("yyyy-MM-dd"), SIMPLE("yyyyMMdd"), FULL(
"yyyy-MM-dd HH:mm:ss"), YMDHM("yyyyMMddHHmm"), MINITUE(
"yyyyMMdd HHmm"), FULL_SECOND("yyyyMMddHHmmss"), YEAR_MONTH(
"yyyyMM"),MONTH("MM"),MONTH_S("M"),MINITUE_SECOND("HHmm"),DATE2("MM/dd/yyyy"),DATE3("yyyy-MM-dd HH:mm"),TIME("HH:mm:ss");
private String format;
private ThreadLocal<SimpleDateFormat> convert;
DateFormat(final String format) {
this.format = format;
convert = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/31会被接受,并转换成2007/03/03
simpleDateFormat.setLenient(false);
return simpleDateFormat;
}
};
}
public String getFormat() {
return format;
}
public ThreadLocal<SimpleDateFormat> getConvert() {
return convert;
}
}
public static Integer retrivePreDate(String date) {
Calendar cal = new GregorianCalendar();
SimpleDateFormat oSdf = new SimpleDateFormat("", Locale.ENGLISH);
oSdf.applyPattern("yyyyMM");
try {
cal.setTime(oSdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
cal.add(Calendar.MONTH, -1);
return Integer.parseInt(DateFormat.YEAR_MONTH.getConvert().get().format(cal.getTime()));
}
public static Integer cauculateActualDay(String date) {
Calendar cal = new GregorianCalendar();
SimpleDateFormat oSdf = new SimpleDateFormat("", Locale.ENGLISH);
oSdf.applyPattern("yyyyMM");
try {
cal.setTime(oSdf.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
public static Long distinctDays(String fromDay, String toDay) {
SimpleDateFormat df = DateFormat.DATE.getConvert().get();
try {
long to = df.parse(toDay).getTime();
long from = df.parse(fromDay).getTime();
return ((to - from) / (1000 * 60 * 60 * 24));
} catch (ParseException e) {
e.printStackTrace();
}
return -1L;
}
public static Boolean verifyDateFormat(String date, DateFormat format) {
try {
format.getConvert().get().parse(date);
return Boolean.TRUE;
} catch (ParseException e) {
return Boolean.FALSE;
}
}
public static String convertDateFormat(Date date, DateFormat format) {
return format.getConvert().get().format(date);
}
public static Date convertDateText(String date, DateFormat format) {
try {
return format.getConvert().get().parse(date);
} catch (ParseException e) {
return null;
}
}
public static Long retriveTodayStartTime() {
// 获取当前服务器12点信息
Calendar currentDate = new GregorianCalendar();
currentDate.set(Calendar.HOUR_OF_DAY, 0);
currentDate.set(Calendar.MINUTE, 0);
currentDate.set(Calendar.SECOND, 0);
return currentDate.getTime().getTime() / 1000 * 1000;
}
public static Date getNextDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 1);
Date date1 = new Date(calendar.getTimeInMillis());
return date1;
}
/**
* 转换string格式的日期为long型
*
* @param date
* @return
*/
public static Long convertSimpleDateToLong(String date) {
try {
return DateFormat.SIMPLE.getConvert().get().parse(date).getTime();
} catch (Exception e) {
// LOG.warn("yyyy-MM-dd HH:mm format failed, user default value");
return -1L;
}
}
/**
* 转换string格式的日期为long型
*
* @param date
* @return
*/
public static Long convertDateToLong(String date) {
try {
return DateFormat.DATE.getConvert().get().parse(date).getTime();
} catch (Exception e) {
return -1L;
}
}
public static Long convertDateToLong(String date, DateFormat format) {
try {
return format.getConvert().get().parse(date).getTime();
} catch (Exception e) {
return -1L;
}
}
/**
* 将日期转换成制定格式的日期
* @param date
* @param format
* @return
*/
public static Date convertDateFormatFromDate(Date date, DateFormat format) {
String date1 = convertDateFormat(date, format);
return convertDateText(date1, format);
}
/**
*将日期字符串转换为另一种格式的日期字符串
* @param date
* @param format
* @return
*/
public static String convertDateTextFromString(String date, DateFormat format,DateFormat format2) {
Date date1 = convertDateText(date, format);
//由于上面这行代码里面catch了转化异常,导致转化失败的时候会直接返回null
//为了防止上面这行代码返回null而导致的下面转化的时候报空指针,这里判断一下,如果为null就直接返回null
if (date1 == null) {
return null;
}
return convertDateFormat(date1, format2);
}
public static void main(String[] args) throws ParseException {
String cmmTime = DateUtils.convertDateFormat(new Date(), DateFormat.SHORT);
System.out.println("cmmTime:" + cmmTime);
System.out.println(convertDateToLong(cmmTime, DateFormat.SHORT));
Date date = DateUtils.convertDateText("2016-03-22", DateFormat.DATE);
// System.out.println("2013,2,1 age:"+getAge(date)+"ageMonth:"+getAgeMonth(date));
// date = DateUtils.convertDateText("2016-02-01",DateFormat.DATE);
// System.out.println("2016,2,1 age:"+getAge(date)+"ageMonth:"+getAgeMonth(date));
// date = DateUtils.convertDateText("2015-08-01",DateFormat.DATE);
// System.out.println("2015,8,1 age:" + getAge(date) + "ageMonth:" + getAgeMonth(date));
System.out.println(DateDiff(new Date(),date));
}
public static Date caculateDateByDay(Date date,Integer value){
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, value);
return cal.getTime();
}
public static String caculateDateByDayForString(String dateStr,Integer value,DateFormat dateFormat){
Date date = DateUtils.convertDateText(dateStr, dateFormat);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, value);
return DateUtils.convertDateFormat(cal.getTime(), dateFormat);
}
// public static String getSeasonOfDate(Date date){
// String month = DateUtils.convertDateFormat(date, DateFormat.MONTH);
// return seasons.get(month);
//
// }
public static String getMonthOfDate(Date date){
String month = DateUtils.convertDateFormat(date, DateFormat.MONTH_S);
return month;
}
public static int getAgeMonth(Date date){
int result = -1;
if(date==null) return -1;
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
int year1 = cal.get(Calendar.YEAR);
int month1 = cal.get(Calendar.MONTH);
cal.setTime(date);
int year2 = cal.get(Calendar.YEAR);
int month2 = cal.get(Calendar.MONTH);
if(year1>year2){
result = (year1-year2)*12 +month1-month2;
}else if(year1<year2){
result = (year2-year1)*12 +month2-month1;
}else{
result = month1-month2;
}
return result;
}
public static int getAge(Date date){
int result = -1;
if(date==null) return -1;
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
int year1 = cal.get(Calendar.YEAR);
cal.setTime(date);
int year2 = cal.get(Calendar.YEAR);
return year1-year2;
}
public static long DateDiff(Date date1 ,Date date2){
long result = 0;
if(date1==null||date2==null) return result;
long timeMills = date1.getTime()-date2.getTime();
result = timeMills/(3600*24*1000);
return result;
}
public static boolean validDateFormat(String str,DateFormat dateFormat){
try{
return str.equals(DateUtils.convertDateTextFromString(str, dateFormat, dateFormat));
}catch(Exception e){
return false;
}
}
/**
* 计算今日剩余秒数
* @return
*/
public static int getTodayRestSecond(){
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
int second = DateUtils.subSecond(cal.getTime(), new Date());
if(second <= 0){
second = 0;
}
return second;
}
/**
* 返回date1-dat2相差的秒数
*
* @param date1
* @param date2
* @return
*/
public static int subSecond(Date date1, Date date2) {
long d1 = date1.getTime();
long d2 = date2.getTime();
int sub = (int) ((d1 - d2) / 1000);
return sub;
}
/**
* 获得几天之后的date
*
* @param date
* @param day
* @return
*/
public static Date getDayAfter(Date date,Integer day){
return new Date(day * 24 * 60 * 60 * 1000+ date.getTime());
}
/**
* 获得几天之前的date
*
* @param date
* @param day
* @return
*/
public static Date getDayBefore(Date date,Integer day){
return new Date(date.getTime() - (24L * 60 * 60 * 1000 * day));
}
/**
* 获得几小时之前的date
*
* @param date
* @param hour
* @return
*/
public static Date getHourBefore(Date date,Integer hour){
return new Date(date.getTime() - (60 * 60 * 1000 * hour));
}
}
07-29
1032
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-11
495
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-22
775
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-04
865
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)