この記事では、参照用のDateUtils Time Toolクラスの特定のコードを共有しています。特定のコンテンツは次のとおりです
パッケージcom.example.administrator.myapp; java.text.parseexceptionをインポートします。 java.text.simpledateformatをインポートします。 java.util.calendarをインポートします。 Import Java.util.date; /*** 2017/12/07にLychunによって作成された日付ツールクラス* */ public class dateutils {/ ***数日前に取得** @param d time* @param day* @return result*/ public static date getDateBefore(date d、int day){calendar now = calendar.getinstance(); now.settime(d); now.set(calendar.date、now.get(calendar.date) - day); now.getTime(); } / ***数日後に時間を取得** @param d time* @param day* @return result* / public static date getdatefter(date d、int day){calendar now = calendar.getinstance(); now.settime(d); now.set(calendar.date、now.get(calendar.date) + day); now.getTime(); } / ** *現在のタイムスタンプ(正確な秒)を取得 * / public static string getCurrtimestamp(){long time = system.currenttimemillis(); string t = string.valueof(time / 1000); tを返します。 } / ** *日付形式の文字列をタイムスタンプに変換します * * @param date_str string * @param形式例:yyyy-mm-dd hh:mm:ss * @return * / public static string2timestamp(string date_str、string format){try {simpledateformat sdf = new simpledateformat(format(format); return string.valueof(sdf.parse(date_str).gettime() / 1000); } catch(Exception e){e.printstacktrace(); } 戻る ""; } / ***日付Timestampに変換* @param date time* @return* / public static string datetimestamp(date date){return string.valueof(date.gettime() / 1000); }/ ** *文字列を日付に変換 * * @param str string * @param format format * @return result */ public static date stringtodate(string str、string format){simpledateformat sdf = new simpledateformat(format); // lowncase mmはsuntes date = nullを表します。 try {date = sdf.parse(str); } catch(parseexception e){e.printstacktrace(); }返品日; } //文字列を日付に変換するpublic static date stringtodate(string str){return stringtodate(str、 "yyyy-mm-dd"); } / ***日付を文字列に変換** @param date time* @param format変換型* @return result* / public static string datetString(date date、string format){simpledateformat sdf = new simpledateformat(format);文字列結果= sdf.format(date);返品結果; } //時刻を毎月の形式に変換しますpublic static string datetString(date date){return dateToString(date、 "yyyy-mm-dd"); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。