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

前端 未结 3 936
执笔经年
执笔经年 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:23

    First you need to parse your date string to a Date and then get the timestamp from that:

    final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
    
    final String myDateString = "someDateString";
    final Date date = dateFormat.parse(myDateString);
    
    final long timestamp = date.getTime();
    

    Have a look at the SimpleDateFormat javadocs for information on which format string to use.

提交回复
热议问题