Java : convert long to Timestamp

前端 未结 4 1726
面向向阳花
面向向阳花 2020-12-31 00:01

I know, how to convert a Timestamp to a long, with the getTime() method.

Is there a method that convert a long to a TimeStamp?

4条回答
  •  攒了一身酷
    2020-12-31 00:29

    This question is a bit old but for those arriving here scratching their heads because their Timestamp object does not include a (long) constructor, Firebase's com.google.firebase.Timestamp object include (Date date) constructor. and (long seconds, int nanoseconds) and Date does include a (long) constructor (which creates a date object based of number of seconds from epoch - same as java.sql.Timestamp

    bottom line is your solution is simply either

    Timestamp(new Date(longEpochTimeVar));
    

    or

    Timestamp(longEpochTimeVar,0);
    

    0 stands for 0 nanoseconds so both option will produce the same result.

提交回复
热议问题