Java: Date from unix timestamp

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

    If you are converting a timestamp value on a different machine, you should also check the timezone of that machine. For example;

    The above decriptions will result different Date values, if you run with EST or UTC timezones.

    To set the timezone; aka to UTC, you can simply rewrite;

        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        java.util.Date time= new java.util.Date((Long.parseLong(timestamp)*1000));
    

提交回复
热议问题