Dieser Artikel teilt den spezifischen Code der Java -Tool -Klasse Dateutils für Ihre Referenz. Der spezifische Inhalt ist wie folgt
Java.Text.ParseException importieren; java.text.SimpledateFormat; importieren java.util.calendar; importieren java.util.date;/*** Beschreibung: öffentliche Datum der Toolklasse*/öffentliche Klasse Datumutils {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 = "yyyyyy monat d Day"; / ** * das aktuelle Datum erhalten * * * @return * */ public static String getCurrentDate () {String datestr = null; SimpleDateFormat df = new SimpledateFormat (DateUtils.date_Format); DEDESTEST = DF.FORMAT (NEUE DATUM ()); DETESTEST REENT; } / ** * Erhalten Sie das aktuelle Datum und die Uhrzeit * * * @return * * / public static String getCurrentDateTime () {String datestr = null; SimpleDateFormat df = new SimpledateFormat (DateUtils.date_time_format); DEDESTEST = DF.FORMAT (NEUE DATUM ()); DETESTEST REENT; } / ** * Erhalten Sie das aktuelle Datum und die Uhrzeit * * * @return * * / public static String getCurrentDateTime (String DateFormat) {String datenStr = null; SimpleDateFormat df = new SimpledateFormat (DateFormat); DEDESTEST = DF.FORMAT (NEUE DATUM ()); DETESTEST REENT; } public static String datetodatetime (Datum Datum) {String datestr = null; SimpleDateFormat df = new SimpledateFormat (DateUtils.date_time_format); DEDESTEST = DF.FORMAT (DATUM); DETESTEST REENT; } / ** * Konvertieren Sie das String -Datum bis zum Datum. } Datum Datum = neues Datum (); SimpleDateFormat df = new SimpledateFormat (DateUtils.date_Format); try {date = df.Parse (datESTR); } catch (ParseException e) {Date = DateUtils.stringtodate (datestern, "yyyymmdd"); } Rückgabedatum; } / ** * Konvertieren Sie das String -Datum bis zum Datum. SimpleDateFormat df = new SimpledateFormat (DateFormat); try {date = df.Parse (datESTR); } catch (parseException e) {e.printstacktrace (); } Rückgabedatum; } / ** * Konvertieren Sie Datum des Datums des Datums in Zeichenfolge * * * @param Datum * @return * * / public static String DateToString (Datum) {String datestr = null; SimpleDateFormat df = new SimpledateFormat (DateUtils.date_Format); DEDESTEST = DF.FORMAT (DATUM); DETESTEST REENT; } / ** * Konvertieren Sie das Datum des Datums des Datums des Datums in Zeichenfolgenformat für Benutzerdefinierte. SimpleDateFormat df = new SimpledateFormat (DateFormat); DEDESTEST = DF.FORMAT (DATUM); DETESTEST REENT; } / ** * den Tageswert des Datums erhalten * * * @param Datum * Geben Sie das Datum ein * @return * * / public static int getdayofdate (Datum) {int d = 0; Kalender cd = calendar.getInstance (); CD.Settime (Datum); d = cd.get (calendar.day_of_month); Rückkehr D; } / ** * Erhalten Sie den Monatswert des Datums * * * @param Datum * das Datum * @return * * / public static int getMonthofDate (Datum) {int m = 0; Kalender cd = calendar.getInstance (); CD.Settime (Datum); M = CD.get (Kalender. Month) + 1; Rückkehr M; } / ** * Erhalten Sie den Jahreswert des Datums * * * @param Datum * das Datum * @return * * / public static int getyearofdate (Datum) {int y = 0; Kalender cd = calendar.getInstance (); CD.Settime (Datum); y = cd.get (calendar.year); kehre y zurück; } / ** * den Tag der Woche erhalten * * * @param Datum * Geben Sie das Datum ein * @return * * / public static int getweekofDate (Datum) {int wd = 0; Kalender cd = calendar.getInstance (); CD.Settime (Datum); wd = cd.get (calendar.day_of_week) - 1; wd zurückkehren; } / ** * Erhalten Sie den ersten Tag des Monats des Eingabedatums * * @param Datum * Eingabedatum * @return * * / public statisches Datum getFirstdayofmonth (Datum) {Kalender cd = calendar.getInstance (); CD.Settime (Datum); CD.Set (Calendar.day_of_month, 1); return cd.getTime (); } / ** * Erhalten Sie den letzten Tag des Monats des Eingabedatums * * @Param Datum * / public statisches Datum getLastdayofmonth (Datum) {Rückgabedatumutils.Addday (DateUtils.getFirstdayofMonth (DateUtils.Addmonth (Datum, 1)), -1); } / ** * Bestimmen Sie, ob es sich um ein Schaltjahr handelt * * * @Param Datum * Eingabedatum * @Return Ja True Nein False * * * / public static boolean isleapyear (Datum) {Kalender cd = calendar.getInstance (); CD.Settime (Datum); int Year = CD.get (Kalender.YEAR); if (Jahr % 4 == 0 && Jahr % 100! = 0 | Jahr % 400 == 0) {return true; } else {return false; }} / ** * Generieren Sie Datumstypformat basierend auf dem Jahr, Monat und Tag, der durch Integer -Nummer * * @param Jahr * Jahr * @param Monat * Monat * @param Day * Day * @return * * / public static Datum getDateByymd (intes Jahr, Intag, Tag) {Kalender CD = Calendar.GetinStance (); CD.Set (Jahr, Monat 1, Tag); return cd.getTime (); } / ** * Erhalten Sie das entsprechende Datum des Jahreszyklus * * @param Datum * Datum eingeben * @param iyear * Die Anzahl der Jahre repräsentiert die vorherige * @return * * / public statisches Datum getyearcycleofdate (Datum, int iyear) {Calendar CD = calendar.getInstance (); CD.Settime (Datum); CD.Add (Calendar.year, iyear); return cd.getTime (); } / ** * Erhalten Sie das entsprechende Datum des Monatszyklus * * @param Datum * Date * @param i * @return * * / public static Datum getMonthCycleofDate (Datum, int i) {calendar cd = calendar.getInstance (); CD.Settime (Datum); CD.ADD (Kalender.month, i); return cd.getTime (); } / ** * Berechnen Sie, wie viele Jahre der Unterschied zwischen Fromdate und Todate * * @param fromdate * @param todate * @return jahrelang * * / public static int getyeArbyminusdate (Datum von Date, Datum toDate) {Kalender df = calendar.getInstance (); df.settime (fromdate); Kalender dt = calendar.getInstance (); dt.settime (todate); return dt.get (calendar.year) -df.get (calendar.year); } / ** * Berechnen Sie, wie viele Monate der Unterschied zwischen Fromdate und Todate * * @param fromdate * @param toDate * @return Monate * * / public static int getMonthByMinusDate (Datum Fromdate, Datum toDate) {Kalender df = calendar.getInstance (); df.settime (fromdate); Kalender 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)); } / ** * Berechnen Sie, wie viele Tage der Unterschied zwischen Fromdate und Todate * * @param fromdate * @param todate * @return tays * * / public static long getaybyMinusDate (Objekt Fromdate, Object toDate) {Datum f = DateUtils.chgobject (Fromdate); Datum t = DateUtils.chgObject (todate); long fd = f.getTime (); long td = t.getTime (); return (td-fd)/(24l * 60l * 60l * 1000L); } / ** * ARTERRÜHREN * * @PARAM BUGFORTION * Datum Datum * @param calcDate * Datumspunkt zu berechnen * @return * * / public static int calcage (Datum Geburtstag, Datum berechnet) {int Cyear = DateUtils.getyearofdate (berechnet); int cmonth = dateUtils.getMonthofdat (berechnet); int cday = dateUtils.getayofdate (berechnet); int byear = dateUtils.getyearofdate (Geburtstag); int bmonth = dateutils.getMonthofdate (Geburtstag); int bday = dateUtils.getayofdate (Geburtstag); if (cmonth> bmonth || (cmonth == Bmonth && cday> Bday)) {return cyear-byear; } else {return cyear-1-byear; }} / ** * das Geburtsdatum von der ID -Karte erhalten * * @param idno * ID -Kartennummer * @return * * / public static String getBirtdayfromidcard (String idno) {calendar cd = calendar.getInstance (); if (idno.length () == 15) {cd.set (Calendar.year, Integer.ValueOf ("19" + idno.substring (6, 8)) .IntValue ()); CD.Set (Calendar.month, Integer.ValueOf (idno.substring (8, 10)) .IntValue () - 1); CD.Set (Calendar.day_of_month, Integer.ValueOf (idno.substring (10, 12)). intValue ()); } else if (idno.length () == 18) {cd.set (Calendar.year, Integer.ValueOf (idno.substring (6, 10)) .IntValue ()); CD.Set (Calendar.month, Integer.ValueOf (idno.substring (10, 12)) .IntValue () - 1); CD.Set (Calendar.day_of_month, Integer.ValueOf (idno.substring (12, 14)). intValue ()); } return dateUtils.datetoString (cd.getTime ()); } / *** Erhöhen Sie (+) oder subtrahieren Sie (-) Tage am Eingabedatum** @param Datum* Eingabedatum* @param Emblem* Anzahl der Tage, um zu erhöhen oder zu verringern CD.Settime (Datum); CD.ADD (Calendar.day_of_month, iday); return cd.getTime (); } / *** add (+) oder subtrahieren Sie (-) Monate am Eingabedatum** @Param Datum* Datum eingeben* @param Bild* Monat, um zu erhöhen oder zu verringern CD.Settime (Datum); CD.ADD (Calendar.month, Bild); return cd.getTime (); } / ** * add (+) oder subtrahieren Sie (-) Jahre am Eingabedatum * * @param Datum * Datum eingeben * @param Emblem * Anzahl der Jahre, um zu erhöhen oder zu verringern CD.Settime (Datum); CD.Add (Calendar.year, iyear); return cd.getTime (); } / ** * Objekttypdatum * @param Datum * @return * / public static Datum chgObject (Objektdatum) {if (Datum! } if (Datum! } return null; } public static long getagebybirthday (String-Datum) {Datum birthday = stringTodate (Datum, "yyyy-mm-dd"); Long Sec = New Date (). GetTime () - birthday.getTime (); langes Alter = Sek./(1000*60*60*24)/365; Rückkehralter; }/** * @param args */public static void main (String [] args) {// String temp = dateUtil.datetoString (getLastdayofmonth (neuer Date ()), //////dateutil.date_format_chinese); // String s = dateUtil.dateToString (DateUtil.Addday (DateUtil.Addyear (neues Datum (), 1),-1)); long s = dateutils.getayByMinusDate ("2012-01-01", "2012-12-31"); System.err.println (s); }}Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.