Question

I'm having a problem in parsing a date. I'm new on android and try to search for the solution but it seems no luck. I'd already tried to follow this one http://developer.android.com/reference/java/text/SimpleDateFormat.html but still got an error. please help me..

I try to parse this date 2014-03-18T02:07:35.742-0400 and try to format to this 03/18/2014 02:07

I got this error:

java.lang.IllegalArgumentException 
at java.text.DateFormat.format(DateFormat.java:361) 
at java.text.Format.format(Format.java:93)
Was it helpful?

Solution

Try something like this:

Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2014-03-18T02:07:35.742-0400");
System.out.println(new SimpleDateFormat("MM/dd/yyyy HH:mm").format(date));

For my time zone prints:

03/18/2014 10:07

OTHER TIPS

private final DateFormat parsedFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.getDefault());
private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());

Date date = dateFormat.parse(dateFormat.format(your_date));
Date parsedDate = parsedFormat.parse(parsedFormat.format(date));

Reference

use following code

try{
        String srcDate = new String("2014-03-18T02:07:35.742-0400");
        SimpleDateFormat srcDf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        SimpleDateFormat destDf = new SimpleDateFormat("yyyy/MM/dd");
        Date date = srcDf.parse(srcDate);
        // format the date into another format
        dateStr = destDf.format(date);


    }catch (ParseException e) {
        // TODO: handle exception
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top