简体   繁体   中英

Convert time to milliseconds using SimpleDateFormat

I am trying to write a utility function which converts the accepts date,timestamp,milliseconds additional to timestamp and return the time in milliseconds. However, I am getting a parse Exception for that.

Example params:

dateJson: 14.11.2016

timestampJson: 21:04:20

millisecsJson: 244

public static long convertToMillisecs(String dateJson, String timestampJson, String millisecsJson) throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss.SSS");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

    String inputString = timestampJson + "." + millisecsJson; 
    Date date = sdf.parse(dateJson + 'T' + inputString);

    return date.getTime();
}

What has to be changed to get the correct parseable date. It is to be noted that I am using a 24 hour clock and I am based in Germany so using UTC in that case is okay ?

14.11.2016 doesn't match dd-MM-yyyy.

And no, Germany is not in the UTC timezone. Use Europe/Berlin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM