Search code examples
javaandroiddatew3c

Convert W3C dateformat to milliseconds android


I have a w3c date format from server 2016-02-13T09:53:49.871Z , I wanted it to convert to milliseconds for working with Time since concept

I get an error java.lang.IllegalArgumentException: Parse error: 2016-02-24T15:01:49+0530

public  String formatTime(String timeFormat) throws Exception{

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
        Date dateServer = simpleDateFormat.parse(timeFormat);

        Calendar cal = Calendar.getInstance();
        cal.setTime(dateServer);
        long timeToFormat=cal.getTimeInMillis();
}

Solution

  • You missed the milliseconds and Z is not recognised as a time zone - you can make it a literal Z:

    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
    Date dateServer = fmt.parse(timeFormat);