Java: Date from unix timestamp

后端 未结 10 1697
北荒
北荒 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:38

    For 1280512800, multiply by 1000, since java is expecting milliseconds:

    java.util.Date time=new java.util.Date((long)timeStamp*1000);
    

    If you already had milliseconds, then just new java.util.Date((long)timeStamp);

    From the documentation:

    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.

提交回复
热议问题