Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
Public final class Dateutils {
/**
* English abbreviations (default) such as: 2014-10-01
*/
public static String Format_short = "YYYY-MM-DD";
/**
* English Name: 2014-10-01-23:10:06
*/
public static String Format_long = "Yyyy-mm-dd HH:mm:ss";
/**
* Accurate to milliseconds full time such as: Yyyy-mm-dd HH:mm:ss. S
*/
public static String Format_full = "Yyyy-mm-dd HH:mm:ss. S ";
/**
* Chinese Shorthand: October 01, 2014
*/
public static String FORMAT_SHORT_CN = "yyyy mm month DD";
/**
* Chinese name: October 01, 2014 23:10 06 seconds
*/
public static String FORMAT_LONG_CN = "yyyy mm month DD day hh" mm min ss sec ";
/**
* Accurate to milliseconds of full Chinese time
*/
public static String FORMAT_FULL_CN = "yyyy mm month DD day hh" mm min ss sec sss millisecond ";
/**
* Get the default date pattern
*/
Public static String Getdatepattern () {
return format_long;
}
/**
* Returns the current date according to the preset format
*
* @return
*/
Public static String Getnow () {
return Format (new Date ());
}
/**
* Returns the current date according to the user format
*
* @param format
* @return
*/
Public static string Getnow (string format) {
return Format (new Date (), format);
}
/**
* formatting dates with preset formatting
*
* @param date
* @return
*/
Public static String format (date date) {
return Format (date, Getdatepattern ());
}
/**
* format date with 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 a few months to the date
*
* @param date
* Date
* @param n
* Number of months to increase
* @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
* Date
* @param n
* Number of days to increase
* @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
* 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
* 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
* 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;
}
}
Java Date Formatting Tool class