この記事では、参照用のJava Date Processing Tooltilsの特定のコードを共有しています。特定のコンテンツは次のとおりです
java.sql.timestampをインポートします。 java.text.parseexceptionをインポートします。 java.text.simpledateformatをインポートします。 java.util.calendarをインポートします。 Import Java.util.date; /*** <日付時間処理ツールクラス>* /public class dateutils { /***日付形式パターンこれがよく使用されます。 */ public static final string pattern_ymd = "yyyy-mm-dd"; /***日付形式パターンこれはよく使用されます。 */ public static final string pattern_ymdhms = "yyyy-mm-dd hh:mm:ss"; /*** YMDパターンに従って指定された日付をフォーマットします。 * * @paramは、フォーマットの日付を日付付けします。 * @return YMDフォーマットされた日付文字列。 * * @see #pattern_ymd */ public static string formatdate(date date){return formatdate(date、pattern_ymd); } /***指定されたパターンに従って指定された日付をフォーマットします。パターン *は、{@link simpledateFormat simple date * format}クラスで使用されるものに準拠する必要があります。 * * @paramは、フォーマットの日付を日付付けします。 * @paramパターン日付のフォーマットに使用するパターン。 * @returnフォーマットされた日付文字列。 * * @Throws ILLEGALARGUMENTEXCEPTION与えられた日付パターンが無効な場合。 * * @see simpledateFormat */ public static string formatdate(date date、string pattern){if(date == null)新しいIllegalargumentexception( "date is null"); if(pattern == null)新しいIllegalargumentException( "pattern is null"); SimpleDateFormat formatter = new SimpledateFormat(pattern); return formatter.format(date); } /***日付値を解析します。日付値の解析に使用される形式は、 *デフォルトのpattern_ymdから取得されます。 * * @param dateValue解析する日付値 * * @ @return return the sarsed date * * @throws与えられたdatevalueが無効である場合。 */ public static date parsedate(string datevalue){return parsedate(datevalue、null); } /***指定された日付形式を使用して日付値を解析します。 * * @param dateValue解析する日付値 * @param dateformat日付形式 * * @@returnの解析日。 Parseが故障した場合、NULL * * @Throws ILLEGALARGUMENTEXCEPTIONを返します。指定された日付値が無効である場合。 */ public static date parsedate(string datevalue、string dateformat){if(datevalue == null){throw new IllegalargumentException( "dateValue is null"); } if(dateformat == null){dateformat = pattern_ymd; } simpledateFormat df = new SimpledateFormat(dateFormat);日付結果= null; try {result = df.parse(datevalue); } catch(parseexception pe){pe.printstacktrace(); // date-type string format error} return result; } /***新しいオブジェクトを返す日付に何年も追加します。 *元の日付オブジェクトは変更されていません。 * * @param日付は、nullではなく日付 * @paramの追加金額をマイナスにする場合があります * @ @ @return with boling added * @throws日付がnull */ public static date addyears(int adt add){return add(date、calendar.year、bolage); } /***新しいオブジェクトを返すタイムスタンプに何年も追加します。 *元のタイムスタンプオブジェクトは変更されていません。 * * @paramタイムスタンプnullではなくタイムスタンプ * @paramの追加金額はマイナスである可能性があります * @ @ @return bed with bult with ded added * @throws Illegalargumentexceptionはnull */ public static Timestamp Addyears(タイムスタンプタイムスタンプ、INT) } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @Paramの追加金額を追加すると、ネガティブになる場合があります * @ @return添加された金額を使用} /***新しいオブジェクトを返すタイムスタンプに数か月を追加します。 *元のタイムスタンプオブジェクトは変更されていません。 * * @paramタイムスタンプタイムスタンプは、nullではなく、追加する金額を否定する可能性があります * @ @ @return bedumput with buld added * @throws Illegalargumentexceptionはnull */ public static Timestamp addMonths(Timestamp Timestamp、Int){return(Timestamp、comleant、month。 } //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Adds a number of days to a date returning新しいオブジェクト。 *元の日付オブジェクトは変更されていません。 * * @param日付は、nullではなく、追加する金額の金額ではなく、ネガティブになる可能性があります * @ @ @return with boling added * @throws日付がnull */ public static date adddays(int bolation){return add(date、calendar.date、bolate); } /***新しいオブジェクトを返すタイムスタンプに数日を追加します。 *元のタイムスタンプオブジェクトは変更されていません。 * * @paramタイムスタンプタイムスタンプは、nullではなく、追加する金額を否定する可能性があります * @ @ @return with buld added * @throws Illegalargumentexception null */ public static Timestamp adddays(タイムスタンプタイムスタンプ、INT) } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------追加 * @Throws IllegalargumentExceptionタイムスタンプがnull */ public staticタイムスタンプ追加(タイムスタンプタイムスタンプ、int額){return add(timestamp、calendar.minute、bomut); } /***新しいオブジェクトを返す現在の時刻に数日を追加します。 * * @paramの追加金額を追加すると、マイナスになる場合があります * @ @return添加された量の新しいタイムスタンプオブジェクト */ public staticタイムスタンプadddays(int bolate){calendar c = calendar.getinstance(); c.add(calendar.date、額);新しいタイムスタンプを返します(c.gettimeinmillis()); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 追加* @paramの追加金額を追加する金額、ネガティブになる場合があります * @ @return bed with bult with boling * @throws日付がnull */ private static add(日付、intカレンダーフィールド、int額){(date == null){新しいIllegalargumentexception( "Null"); }カレンダーc = charend.getInstance(); c.settime(日付); c.add(カレンダーフィールド、金額); c.getTimeを返します(); } /***新しいオブジェクトを返すタイムスタンプに追加します。 *元のタイムスタンプオブジェクトは変更されていません。 * * @paramタイムスタンプタイムスタンプ、nullではなく * @paramカレンダーフィールドに追加するカレンダーフィールド * @paramの金額を追加する量になります。 IllegalargumentException( "タイムスタンプはnullであってはなりません"); }カレンダーc = charend.getInstance(); c.settime(タイムスタンプ); c.add(カレンダーフィールド、金額);新しいタイムスタンプを返します(c.gettimeinmillis()); } / *** <1日の最小日付値を生成>* @return dayの最低日付値c.set(calendar.hour_of_day、0); c.set(calendar.minute、0); c.set(Calendar.second、0); c.set(calendar.millisecond、0);新しいタイムスタンプを返します(c.gettimeinmillis()); } /**このクラスはインスタンス化されないでください。 */ privatedateutils(){}}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。