The example of this article for you to share the Java tool class Dateutils specific code for your reference, the specific content as follows
Import Java.text.parseexception;import java.text.simpledateformat;import Java.util.calendar;import java.util.Date ;/** * Description: Common Date Tool Class */public class Dateutils {public static String Date_format = "YYYY-MM-DD"; public static String Date_time_format = "Yyyy-mm-dd HH:mm:ss"; public static String Date_format_chinese = "yyyy year m D Day"; /** * Get Current date * * * @return * */public static string Getcurrentdate () {string datestr = null; SimpleDateFormat df = new SimpleDateFormat (Dateutils.date_format); Datestr = Df.format (New Date ()); return datestr; }/** * Gets the current datetime * * * @return * */public static string GetCurrentDateTime () {string datestr = null; SimpleDateFormat df = new SimpleDateFormat (Dateutils.date_time_format); Datestr = Df.format (New Date ()); return datestr; }/** * Gets the current datetime * * * @return * */public static string GetCurrentDateTime (String dateformat) {Strin G datestr = null; SimpleDateFormat df = new simpledAteformat (DateFormat); Datestr = Df.format (New Date ()); return datestr; public static string Datetodatetime (date date) {string datestr = null; SimpleDateFormat df = new SimpleDateFormat (Dateutils.date_time_format); Datestr = Df.format (date); return datestr; /** * Convert string date to date format * * * @param datestr * @return * */public static date Stringtodate (String datest R) {if (Datestr ==null | | Datestr.equals ("")) {return null; } Date Date = new Date (); SimpleDateFormat df = new SimpleDateFormat (Dateutils.date_format); try {date = Df.parse (DATESTR); } catch (ParseException e) {date=dateutils.stringtodate (datestr, "yyyyMMdd"); } return date; /** * Convert string date to date format * Custom format * * @param datestr * @return * * * public static date stringtodate (String d Atestr, String dateformat) {Date date = new Date (); SimpleDateFormat df = new SimpleDateFormat (DateFormat); try {date = Df.parse (datestR); } catch (ParseException e) {e.printstacktrace (); } return date; /** * Convert date format date to string format * * * @param date * @return * */public static string datetostring (date date) { String datestr = null; SimpleDateFormat df = new SimpleDateFormat (Dateutils.date_format); Datestr = Df.format (date); return datestr; /** * Convert date format date to string format custom format * * @param date * @param dateformat * @return */public static string Datetos Tring (date date, string dateformat) {string datestr = null; SimpleDateFormat df = new SimpleDateFormat (DateFormat); Datestr = Df.format (date); return datestr; /** * Gets the day value of the date * * * @param date * Enter Date * @return * */public static int getdayofdate (Date da TE) {int d = 0; Calendar cd = Calendar.getinstance (); Cd.settime (date); D = Cd.get (Calendar.day_of_month); return D; }/** * Gets the month value of the date * * * @param date * Enter Date * @return * */Public STATIC int getmonthofdate (date date) {int m = 0; Calendar cd = Calendar.getinstance (); Cd.settime (date); m = Cd.get (calendar.month) + 1; return m; /** * Gets the year value of the date * * * @param date * Enter Date * @return * */public static int getyearofdate (date date) {int y = 0; Calendar cd = Calendar.getinstance (); Cd.settime (date); y = Cd.get (calendar.year); return y; /** * Get Day of the week * * * @param date * Enter Date * @return * */public static int getweekofdate (date date) {int WD = 0; Calendar cd = Calendar.getinstance (); Cd.settime (date); WD = Cd.get (Calendar.day_of_week)-1; return WD; }/** * Gets the first day of the month entered date * * * @param date * Enter Date * @return * */public static date Getfirstdayofmon Th (date date) {Calendar cd = Calendar.getinstance (); Cd.settime (date); Cd.set (Calendar.day_of_month, 1); return Cd.gettime (); }/** * Gets the last day of the month entered date * * @param date */public static DaTe getlastdayofmonth (date date) {return Dateutils.addday (Dateutils.getfirstdayofmonth (Dateutils.addmonth (date, 1)), -1); }/** * To determine if it is a leap year * * * @param date * Enter Date * @return is true no false * */public static Boolean Islea Pyear (date date) {Calendar cd = Calendar.getinstance (); Cd.settime (date); int year = Cd.get (calendar.year); if (year% 4 = = 0 && year%! = 0 | year% = = 0) {return true; } else {return false; }}/** * Generates a date type format based on the number of integers represented by the integer type * * @param year * @param month * month * @param days * Day * @return * */public static Date Getdatebyymd (int year, int month, Int. day) {Calendar cd = Calendar.getinstanc E (); Cd.set (year, month-1, day); return Cd.gettime (); }/** * Gets the year cycle corresponding day * * @param date * Input date * @param iyear * years negative number before * @return * */Public Static date getyearcycleofdate (date date, int iyear) {Calendar cd = Calendar.getinStance (); Cd.settime (date); Cd.add (Calendar.year, iyear); return Cd.gettime (); /** * Get month cycle corresponding Day * * @param date * Enter Date * @param i * @return * */public static date Getmonthcycl Eofdate (date date, int i) {Calendar cd = Calendar.getinstance (); Cd.settime (date); Cd.add (Calendar.month, i); return Cd.gettime (); }/** * Calculates the difference in FromDate to toDate years * * @param fromdate * @param toDate * @return Years * */public static I NT Getyearbyminusdate (date fromdate, date toDate) {Calendar df=calendar.getinstance (); Df.settime (FromDate); Calendar dt=calendar.getinstance (); Dt.settime (toDate); Return Dt.get (calendar.year)-df.get (calendar.year); }/** * Calculates how many months difference fromdate to toDate * * @param fromdate * @param toDate * @return months * */public static int getmonthbyminusdate (date fromdate, date toDate) {Calendar df=calendar.getinstance (); Df.settime (FromDate); Calendar dt=calendar.getinstance (); Dt.setTime (toDate); Return Dt.get (calendar.year) *12+dt.get (calendar.month)-(Df.get (calendar.year) *12+df.get (calendar.month)); }/** * Calculates the difference in fromdate to toDate days * * @param fromdate * @param toDate * @return Days * */public static L Ong getdaybyminusdate (Object FromDate, Object toDate) {Date f=dateutils.chgobject (fromdate); Date T=dateutils.chgobject (toDate); Long Fd=f.gettime (); Long Td=t.gettime (); Return (TD-FD)/(24L * 60L * 60L * 1000L); /** * Calculate Age * * @param birthday * Birthday Date * @param calcdate * Date point to calculate * @return * */Public static int calcage (date birthday, date calcdate) {int cyear=dateutils.getyearofdate (calcdate); int cmonth=dateutils.getmonthofdate (calcdate); int cday=dateutils.getdayofdate (calcdate); int byear=dateutils.getyearofdate (birthday); int bmonth=dateutils.getmonthofdate (birthday); int bday=dateutils.getdayofdate (birthday); if (cmonth>bmonth| | (CMONTH==BMONTH&&CDAY>bday)) {return cyear-byear; }else{return cyear-1-byear; }}/** * Get birth date from ID * * @param idno * ID Number * @return * */public static String Getbirthdayfromi Dcard (String idno) {Calendar cd = Calendar.getinstance (); if (idno.length () = =) {Cd.set (Calendar.year, integer.valueof ("+" + idno.substring (6, 8)). Intvalue ()); Cd.set (Calendar.month, Integer.valueof (idno.substring (8)). Intvalue ()-1); Cd.set (Calendar.day_of_month, integer.valueof (Idno.substring (Ten)). Intvalue ()); } else if (idno.length () = =) {Cd.set (Calendar.year, Integer.valueof (idno.substring (6)). Intvalue ()); Cd.set (Calendar.month, integer.valueof (Idno.substring (Ten)). Intvalue ()-1); Cd.set (Calendar.day_of_month, Integer.valueof (idno.substring ()). Intvalue ()); } return Dateutils.datetostring (Cd.gettime ()); /** * Add (+) or subtract (-) days on Input date * * @param datE * Enter Date * @param imonth * Number of days to increase or decrease */public static date Addday (date date, int iday) {Calendar CD = Calendar.getinstance (); Cd.settime (date); Cd.add (Calendar.day_of_month, iday); return Cd.gettime (); /** * Add (+) or subtract (-) month on input date * * @param date * Enter Date * @param imonth * Month score to increase or decrease */public Stati C Date Addmonth (date date, int imonth) {Calendar cd = Calendar.getinstance (); Cd.settime (date); Cd.add (Calendar.month, imonth); return Cd.gettime (); /** * Increase (+) or subtract (-) year on input date * * @param date * Enter Date * @param imonth * Number of years to increase or decrease */public static Date Addyear (date date, int iyear) {Calendar cd = Calendar.getinstance (); Cd.settime (date); Cd.add (Calendar.year, iyear); return Cd.gettime (); }/** * Transforms the Object type to date * @param date * @return */public static date Chgobject (object date) {if (date!=null& Amp;&date instanceof Date) {return (date) date; } if (date!=null&Amp;&date instanceof String) {return dateutils.stringtodate ((String) date); } return null; public static long Getagebybirthday (String date) {Date birthday = Stringtodate (date, "Yyyy-mm-dd"); Long sec = new Date (). GetTime ()-birthday.gettime (); Long age = sec/(1000*60*60*24)/365; return age; }/** * @param args */public static void main (string[] args) {//string temp = dateutil.datetostring (getlastdayo Fmonth (New Date ()),///Dateutil.date_format_chinese); String s=dateutil.datetostring (Dateutil.addday (Dateutil.addyear (New Date (), 1),-1)); Long S=dateutils.getdaybyminusdate ("2012-01-01", "2012-12-31"); System.err.println (s); }}
http://www.pdfxs.com/search?q=WANZ-254
Java Tools Class Dateutils examples