How to convert “YYYY-MM-DD hh:mm:ss” to UNIX timestamp in Java?

前端 未结 3 961
执笔经年
执笔经年 2020-12-20 09:59

Or, maybe you know a better approach how to solve this problem. I get dates in the format of \"YYYY—MM-DD HH:MM:SS\" and need to calculate time difference between now then i

3条回答
  •  遥遥无期
    2020-12-20 10:18

    You need to convert your string into java.util.Date with the assistance of SimpleDateFormat (for examples see - https://stackoverflow.com/search?q=simpledateformat+%5Bjava%5D) and then get the number of milliseconds since January 1, 1970, 00:00:00 GMT.

    Date dt = new Date();
    long unixTimeStamp = dt.getTime();
    

提交回复
热议问题