Javaは、UNIXタイムスタンプを参照用に指定された形式の日付に変換します。特定のコンテンツは次のとおりです
サーバーからデータを取得する場合、取得したデータの時間が多くの場合、タイムスタンプがこれに似ている場合があります。もちろん、タイムスタンプの形でこのデータをユーザーに提示することは不可能です。通常、このタイムスタンプを一連の処理で処理して、閲覧に慣れている形式にして慣れている必要があります。では、これらのタイムスタンプ形式のデータをどのように処理しますか?各言語とフレームワークには、独自の方法と方法があります。
以下はJavaで実装されます。ナンセンスを言わずにコードを読みましょう...
メソッドの実装
/*** Javaは、Unixタイムスタンプを指定された形式の日付文字列に変換します* @Param TimestAmptringタイムスタンプ "1473048265"; * @paramフォーマットフォーマットのフォーマットデフォルト: "yyyy-mm-dd hh:mm:ss"; * * @return return result: "2016-09-05 16:06:42"; */ public static string timestamp2date(string timestampstring、string formats){if(textutils.isempty(formats))formats = "yyyy-mm-dd hh:mm:ss"; LONG TIMESTAMP = LONG.PARSELONG(タイムスタンプストリング) * 1000; string date = new SimpledateFormat(Formats、locale.china).format(new Date(Timestamp));返品日; }呼び出し方法
Timestamp2date( "1473048265"、 "yyyy-mm-dd hh:mm:ss");
返品結果
2016-09-05 16:06:42
Java指定されたフォーマット日付をUnixタイムスタンプに変換します
/ ** *日付形式の文字列をタイムスタンプに変換 * * * @param datestr string date * @param形式:yyyy-mm-dd hh:mm:ss * * @return */ public static string date2timestamp(string datestr、string format){try {simpledateformat sdf = new simpledateformat(format); return string.valueof(sdf.parse(datestr).gettime() / 1000); } catch(Exception e){e.printstacktrace(); } 戻る ""; }現在のタイムスタンプを入手してください(正確に秒)
/ ** *現在のタイムスタンプを取得します(正確に秒) * * @return nowtimestamp */ public static string getnowtimestamp(){long time = system.currenttimemillis(); string nowtimestamp = string.valueof(time / 1000); Nowtimestampを返します。 }上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。