How to convert timestamp string to epoch time?

后端 未结 4 1475
离开以前
离开以前 2021-01-20 00:29

I have time stamp in format 2017-18-08 11:45:30.345.
I want to convert it to epoch time, so I am doing below:

String timeDateStr = \"2017         


        
4条回答
  •  旧巷少年郎
    2021-01-20 01:04

    Corrected your code regarding yyyy-dd-MM. Also minute value could be 1-59 not 60. You provided 60. This is another simple way to solve the issue. Simply use DateFormat class.

    String timeDateStr = "2017-18-08 12:59:30.345";
    DateFormat df = new SimpleDateFormat("yyyy-dd-MM hh:mm:ss.SSS", Locale.ENGLISH);
    try {
        Date d = df.parse(timeDateStr);
        System.out.println(d.toInstant().toEpochMilli());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    

提交回复
热议问题