Copy the code code as follows:
/**
*Date tool class
* By default, "yyyy-MM-dd HH:mm:ss" is used to format the date
*/
public final class DateUtils {
/**
* English abbreviation (default) such as: 2010-12-01
*/
public static String FORMAT_SHORT = "yyyy-MM-dd";
/**
* The full English name is: 2010-12-01 23:15:06
*/
public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
/**
* Complete time accurate to milliseconds such as: yyyy-MM-dd HH:mm:ss.S
*/
public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
/**
*Chinese abbreviation such as: December 01, 2010
*/
public static String FORMAT_SHORT_CN = "yyyy year MM month dd";
/**
* The full Chinese name is: December 01, 2010, 23:15:06
*/
public static String FORMAT_LONG_CN = "yyyy year MM month dd day HH hour mm minute ss second";
/**
* Complete Chinese time accurate to milliseconds
*/
public static String FORMAT_FULL_CN = "yyyy year MM month dd day HH hour mm minute ss second SSS millisecond";
/**
* Get the default date pattern
*/
public static String getDatePattern() {
return FORMAT_LONG;
}
/**
* Return the current date according to the preset format
* @return
*/
public static String getNow() {
return format(new Date());
}
/**
* Return the current date according to user format
* @param format
* @return
*/
public static String getNow(String format) {
return format(new Date(), format);
}
/**
* Format dates using preset formats
* @param date
* @return
*/
public static String format(Date date) {
return format(date, getDatePattern());
}
/**
* Format date using user format
* @param date date
* @param pattern date format
* @return
*/
public static String format(Date date, String pattern) {
String returnValue = "";
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return (returnValue);
}
/**
* Extract string date using preset format
* @param strDate date string
* @return
*/
public static Date parse(String strDate) {
return parse(strDate, getDatePattern());
}
/**
* Extract string date using user format
* @param strDate date string
* @param pattern date format
* @return
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* Add several whole months to the date
* @param date date
* @param n The number of months to add
* @return
*/
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
/**
* Add days to date
* @param date date
* @param n The number of days to add
* @return
*/
public static Date addDay(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, n);
return cal.getTime();
}
/**
* Get timestamp
*/
public static String getTimeString() {
SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
}
/**
* Get date year
* @param date date
* @return
*/
public static String getYear(Date date) {
return format(date).substring(0, 4);
}
/**
* According to the default format of the string, the number of days from today
* @param date date string
* @return
*/
public static int countDays (String date) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date));
long t1 = c.getTime().getTime();
return (int)(t/1000 - t1/1000)/3600/24;
}
/**
* Number of days from today according to user format string
* @param date date string
* @param format date format
* @return
*/
public static int countDays (String date, String format) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date, format));
long t1 = c.getTime().getTime();
return (int)(t/1000 - t1/1000)/3600/24;
}
}