Formatdate. Java
Package COM. ucit. CA. webapp. tool; import Java. util. *; import Java. text. *; public class formatdate {public formatdate () {}// format the date as the string "yyyy-mm-dd hh: mm" Public String formatdatetime (date basicdate, string strformat) {simpledateformat df = new simpledateformat (strformat); Return DF. format (basicdate);} // format the date as the string "yyyy-mm-dd hh: mm" Public String formatdatetime (string basicdate, string strformat) {simpledateformat df = new simpledateformat (strformat); Date tmpdate = NULL; try {tmpdate = DF. parse (basicdate);} catch (exception e) {// Date string format error} return DF. format (tmpdate);} // date after the current date plus or minus n days, returns string (yyyy-mm-dd) Public String ndaysaftertoday (int n) {simpledateformat df = new simpledateformat ("yyyy-mm-dd"); Calendar rightnow = calendar. getinstance (); // rightnow. add (calendar. day_of_month,-1); rightnow. add (calendar. day_of_month, + n); Return DF. format (rightnow. gettime ();} // The date after the current date plus or minus n days, returns string (yyyy-mm-dd) Public date ndaysafternowdate (int n) {calendar rightnow = calendar. getinstance (); // rightnow. add (calendar. day_of_month,-1); rightnow. add (calendar. day_of_month, + n); Return rightnow. gettime () ;}// if a date string is specified, the Public String ndaysafteronedatestring (string basicdate, int N) of the date string plus or minus n days is returned) {simpledateformat df = new simpledateformat ("yyyy-mm-dd"); Date tmpdate = NULL; try {tmpdate = DF. parse (basicdate);} catch (exception e) {// Date string format error} Long nday = (tmpdate. gettime ()/(24*60*60*1000) + 1 + n) * (24*60*60*1000); tmpdate. settime (nday); Return DF. format (tmpdate);} // for a given date, return the public date ndaysafteronedate (date basicdate, int N) {long nday = (basicdate. gettime ()/(24*60*60*1000) + 1 + n) * (24*60*60*1000); basicdate. settime (nday); Return basicdate;} // calculate the number of days between two dates. Public int ndaysbetweentwodate (date firstdate, date seconddate) {int nday = (INT) (seconddate. gettime ()-firstdate. gettime ()/(24*60*60*1000); Return nday;} // calculate the number of days between two dates. Public int ndaysbetweentwodate (string firststring, string secondstring) {simpledateformat df = new simpledateformat ("yyyy-mm-dd"); Date firstdate = NULL; Date seconddate = NULL; try {firstdate = DF. parse (firststring); seconddate = DF. parse (secondstring);} catch (exception e) {// Date string format error} int nday = (INT) (seconddate. gettime ()-firstdate. gettime ()/(24*60*60*1000); Return nday ;}}
Source code:
Http://cid-6adab3222e64b22c.skydrive.live.com/self.aspx/.Public/%e4%bb%a3%e7%a0%81/javaToolClass/FormatDate.java