2012-08-02 5 views
18

Long値をStringまたはDateに変換する場合は、この形式のdd/mm/YYYYに変換します。長形式からの変換

私は長い形式でこの値を持っている:1343805819061.

日付形式に変換することは可能でしょうか?

ありがとうございました。

答えて

51

以下のコード行を使用してこれを行うことができます。ここでは、timeInMilliSecondはlong値です。

String dateString = new SimpleDateFormat("MM/dd/yyyy").format(new Date(TimeinMilliSeccond)); 

また、以下のコードも使用できます。

String longV = "1343805819061"; 
long millisecond = Long.parseLong(longV); 
// or you already have long value of date, use this instead of milliseconds variable. 
String dateString = DateFormat.format("MM/dd/yyyy", new Date(millisecond)).toString(); 

参考: - DateFormatSimpleDateFormat

P.S.必要に応じて日付形式を変更します。

4

DateインスタンスでメソッドsetTimeを使用するか、日付コンストラクタでDate(long)を使用できます。その後

setTime(long time) 
     Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT. 

Date(long date) 
     Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT 

http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/text/DateFormatter.html

2
java.util.Date dateObj = new java.util.Date(timeStamp); 

を参照してくださいフォーマッタここtimeStampには今、あなたはJavaの日付オブジェクトを取得 、実際milliesecondsのタイムスタンプである、あなたの長整数であるシンプルな日付を使用これで文字列に変換できます

SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("yyyyMMdd"); 
SimpleDateFormat dateformatMMDDYYYY = new SimpleDateFormat("MMddyyyy"); 

StringBuilder nowYYYYMMDD = new StringBuilder(dateformatYYYYMMDD.format(dateObj)); 
StringBuilder nowMMDDYYYY = new StringBuilder(dateformatMMDDYYYY.format(dateObj)); 
関連する問題