Copy Code code as follows:
/**
* Date Tool Class
* The default use of the "YYYY-MM-DD HH:mm:ss" format date
*/
Public final class Dateutils {
/**
* English abbreviation (default) such as: 2010-12-01
*/
public static String Format_short = "YYYY-MM-DD";
/**
* English full name: 2010-12-01 23:15:06
*/
public static String Format_long = "Yyyy-mm-dd HH:mm:ss";
/**
* Accurate to millisecond full time such as: Yyyy-mm-dd HH:mm:ss. S
*/
public static String Format_full = "Yyyy-mm-dd HH:mm:ss. S ";
/**
* Chinese abbreviation: December 01, 2010
*/
public static String FORMAT_SHORT_CN = "yyyy year mm month DD";
/**
* Chinese Full name: December 01, 2010 23:15 06 seconds
*/
public static String FORMAT_LONG_CN = "yyyy mm month DD Day hh when the minute of SS seconds";
/**
* Accurate to millisecond full Chinese time
*/
public static String FORMAT_FULL_CN = "yyyy mm month DD Day hh when the minute is SS seconds sss milliseconds";
/**
* Get the default date pattern
*/
public static String Getdatepattern () {
return format_long;
}
/**
* Return current date according to preset format
* @return
*/
public static String Getnow () {
Return format (new Date ());
}
/**
* Return current date according to user format
* @param format
* @return
*/
public static string Getnow (string format) {
Return format (new Date (), format);
}
/**
* Format dates with preset formatting
* @param date
* @return
*/
public static String format (date date) {
return format (date, Getdatepattern ());
}
/**
* Format dates with user format
* @param 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 a few month to the date
* @param date
* @param n to increase the number of months
* @return
*/
public static date Addmonth (date date, int n) {
Calendar cal = Calendar.getinstance ();
Cal.settime (date);
Cal.add (Calendar.month, N);
return Cal.gettime ();
}
/**
* Increase the number of days on the date
* @param date
* @param n to increase the number of days
* @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 time stamp
*/
public static String gettimestring () {
SimpleDateFormat df = new SimpleDateFormat (format_full);
Calendar calendar = Calendar.getinstance ();
Return Df.format (Calendar.gettime ());
}
/**
* Get date year
* @param date
* @return
*/
public static String getyear (date date) {
return format (date). substring (0, 4);
}
/**
* By default format string distance today's number of days
* @param 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;
}
/**
* by user format string distance today's days
* @param date string
* @param format Date
* @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;
}
}