0

I am reciving a input in this format 2012-01-13T00:00:00.000-05:00 and which i need to convert this into yyyyMMdd Format . I have also set the SimpleDateFormat.setLenient(false);

This is my coding for parsing the Date

public static String getparsedDate(String date) throws Exception {
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US);
        sdf.setLenient(false);

        String s1 = date;
        String s2 = null;
        Date d;
        try {
            d = sdf.parse(s1);
            s2 = (new SimpleDateFormat("yyyyMMdd")).format(d);

        } catch (ParseException e) {

            e.printStackTrace();
        }

        return s2;

    }

But i am getting a Exception at

java.text.ParseException: Unparseable date: "201201"

at java.text.DateFormat.parse(Unknown Source)

Could anybody please let me know , what might be the issue ?

Pawan
  • 31,545
  • 102
  • 256
  • 434

2 Answers2

0

You are missing the timezone in your format string. If you check the argument, it is finishing with -05:00 and you are also using Lenient==false.

Luis
  • 1,294
  • 7
  • 9
  • Thanks for the response , but how can you know what to be passed exactly ? – Pawan Mar 01 '12 at 11:32
  • I don't understand your question... the input is defined by someone else, I guess, so they should tell you. What the format expects is the first parameter of the constructor. See http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat(java.lang.String, java.util.Locale) – Luis Mar 01 '12 at 11:42
0

Unfortunately, the time zone formats available to SimpleDateFormat are not ISO8601 compliant. SimpleDateFormat understands time zone strings like "GMT+01:00" or "+0100", the latter according to RFC822. Therefore using SimpleDateFormat does not seem as an option in your case (since you use UTC−05:00 as timezone).

Instead of SimpleDateFormat you need to use JodaTime for that type of date format.

Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
  • Thanks for the answer , but i am getting java.lang.IllegalArgumentException: Illegal pattern character 'Y' at java.text.SimpleDateFormat.compile(Unknown Source) at java.text.SimpleDateFormat.initialize(Unknown Source) at java.text.SimpleDateFormat.(Unknown Source) – Pawan Mar 01 '12 at 12:05
  • I have edited my answer. I seriously recommend you to take a look at JodaTime API. Check this discussion for further information: http://stackoverflow.com/questions/2201925/converting-iso8601-compliant-string-to-java-util-date – Korhan Ozturk Mar 01 '12 at 12:10
  • Thanks , but we were not suppoused to use Third Party API , anyway Thanks once again . – Pawan Mar 01 '12 at 12:22