2016-05-05 32 views
0

タイムスタンプ値444663422.631236を現在の日付に変換する方法。私はこれらの方法を試しましたが、うまくいきません。タイムスタンプを日付に変換する

public class Teste { 
public static void main(String[] args) throws ParseException { 
    String l = "480364794"; 
    String d = "479617200"; 

    print(convertEpoc("444663422")); 

    print(convert("480364794", "dd/MM/yyyy HH:mm:ss")); 
    print(convert("479617200", "dd/MM/yyyy HH:mm:ss")); 

} 

private static String convertEpoc(String d) { 
    long epoch = Long.parseLong(d); 
    Instant instant = Instant.ofEpochMilli(epoch * 1000); 
    ZonedDateTime.ofInstant(instant, ZoneOffset.UTC); 
    return instant.toString(); 
} 

public static String convert(String epochSec, String dateFormatStr) { 
    Date date = new Date(Long.parseLong(epochSec) * 1000); 
    SimpleDateFormat format = new SimpleDateFormat(dateFormatStr, 
      Locale.getDefault()); 
    return format.format(date); 
} 

public static void print(String s) { 
    System.out.println(s); 
}} 

何が間違っていますか?

+0

'convert'はOKで、' convertEpoc'ではどのようなJARを使用しましたか? – Blank

+0

@JoséHolandaNeto「働いていない」とはどういう意味ですか?あなたは何を期待しましたか、代わりに何を得ましたか? –

+0

@JoséHolandaNeto最初の文章では入力の例をいくつか与えていますが、次に例のコードでは全く異なる入力を与えるのはなぜですか?投稿する前にあなたの考えを整理するために働いてください。 –

答えて

0

あなたの質問は2種類の入力と矛盾しています。最初に1秒間を見てから、入力を小数点以下の数値で見ます。

全体秒

入力がepoch reference dateからの秒数であるとき、千を乗じの必要はありません。 Instantクラスは静的メソッドofEpochSecondを使用して秒単位で処理します。

OffsetDateTime

のみoffset-from-UTC(又はUTC自体)を使用する場合、OffsetDateTimeクラスを使用します。 ZonedDateTimeクラスは、フルタイムゾーンを割り当てるためのクラスです。フルタイムゾーンはoffset-from-UTCのような異常を処理するための規則のセットです(Daylight Saving Time (DST)など)。

したがって、オフセットとして割り当てられるInstantOffsetDateTimeの違いは何ですか?意味の違いはなく、どちらも同じ瞬間を表しています。しかし、OffsetDateTimeには追加機能があります。そのような機能の1つは、文字列表現を生成するときの柔軟な書式設定ですが、Instantは、独自のtoStringメソッド以外の書式設定を意図していません。

String input = "480364794"; 
Instant instant = Instant.ofEpochSecond (Long.parseLong (input)); 
OffsetDateTime odt = OffsetDateTime.ofInstant (instant , ZoneOffset.UTC); 

System.out.println ("input: " + input + " | instant: " + instant + " | odt: " + odt); 

実行時。

入力:480364794 |インスタント:1985-03-22T18:39:54Z | ODT:1985-03-22T18:39:小数第二の入力のため

小数秒

54Z、画分からの秒の整数を分離します。次に、静的ファクトリメソッドInstant.ofEpochSecondにそれぞれ2つの引数を渡します。

これらの番号を取得する方法の1つは、BigDecimalです。 double/floatを使用しないでください。これらのデータ型は実行速度の精度を犠牲にするためです。

BigDecimalの小数部の抽出については、this Questionを参照してください。特にthis Answerthis Answerに注意してください。

BigDecimal input = new BigDecimal ("444663422.631236"); 

// Seconds 
long seconds = input.longValue(); // = 444,663,422. Truncates the fraction. Only the whole number portion remains. 

// Nanoseconds 
BigDecimal fractionalSecond = input.remainder (BigDecimal.ONE); // = 0.631236. Truncates whole number. Only decimal fraction remains. 
int decimalPlacesForNanoseconds = 9; 
if (fractionalSecond.scale() > decimalPlacesForNanoseconds) { // Too many decimal places? 
    // FIXME: Handle this error condition. This means data loss as our input has a granularity finer than nanoseconds. 
    System.out.println ("ERROR - Input finer than nanoseconds. Message # 0aa71e3b-6a5b-4ac4-b239-68e6b9f12284."); 
} 
BigInteger nanosBI = fractionalSecond.movePointRight (decimalPlacesForNanoseconds).toBigInteger(); // Make a whole number from the fraction. 
long nanos = nanosBI.longValue(); 

// Instant = (Seconds + Nanoseconds) 
Instant instant = Instant.ofEpochSecond (seconds , nanos); 

コンソールにダンプします。

System.out.println ("input: " + input + " | seconds: " + seconds + " | nanos: " + nanos + " | instant: " + instant); 

実行時。

入力:444663422.631236 |秒:444663422 | nanos:631236000 |インスタント:1984-02-03T13:37:02。631236Z

関連する問題