Java: Date from unix timestamp

后端 未结 10 1566
北荒
北荒 2020-11-22 04:10

I need to convert a unix timestamp to a date object.
I tried this:

java.util.Date time = new java.util.Date(timeStamp);

Timestamp value

10条回答
  •  有刺的猬
    2020-11-22 04:24

    Sometimes you need to work with adjustments.

    Don't use cast to long! Use nanoadjustment.

    For example, using Oanda Java API for trading you can get datetime as UNIX format.

    For example: 1592523410.590566943

        System.out.println("instant with nano = " + Instant.ofEpochSecond(1592523410, 590566943));
        System.out.println("instant = " + Instant.ofEpochSecond(1592523410));
    

    you get:

    instant with nano = 2020-06-18T23:36:50.590566943Z
    instant = 2020-06-18T23:36:50Z
    

    Also, use:

     Date date = Date.from( Instant.ofEpochSecond(1592523410, 590566943) );
    

提交回复
热议问题