3 Replies Latest reply on Jul 5, 2012 11:55 AM by Pascal B

    Parsing date from JSON response

      Is there any helper method to parse date time value being returned in JSONResponse to get java.util.Date. For example, 2012-06-26T15:51:59.078-05:00. I have done simple java method using SimpleDateFormat, but that does not handle all possible formats. Anyone has ideas on libraries that can be used for this purpose?

        • 1. Re: Parsing date from JSON response
          Pascal B
          What is the string that is being sent via the JSONResponse? The simpleDateFormat is the class supposed to be used to transform strings to Date or GregorianCalendar(i would advise you to use this one, since it has less deprecated methods).
          Is this what you get in the JSON? "2012-06-26T15:51:59.078-05:00" (the one you posted) or is this the format you want to achieve?

          If you try this:

          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
          String JSONDate = "2012-06-26T15:51:59.078-0500";
          GregorianCalendar datum = new GregorianCalendar();

          try {
          } catch (ParseException e) {

          It should transform the string you provided (if you leave the ":" in the time zone away, its either GMT-05:00 or just -0500) to a GregorianCalendar object without loosing any information.

          Or did you mean you want all the possible formats returned in a JSON response to be transformed into a Date object? Then I would wonder why there are so many different forms of date representation in your system. However you should still always be able to use SimpeDateFormat to get from the String representation to the java provided objects and back.

          I hope I was able to help,
          • 2. Re: Parsing date from JSON response
            I was seeing these two formats.


            I see this difference, based on whether underlying date has milliseconds precision or not.

            I would expect to see following, if Rest Service was running in GMT timezone.


            If we use services provided by other vendors, we may see these and other variations, just like what you can expect in SOAP Web Services.
            • 3. Re: Parsing date from JSON response
              Pascal B
              Oh I see you receive different formats from different services, who's backend you can not modify.

              I found this, it should help you get the job done:

              Note however, that his problem is only relatively simplistic, yours on the other hand seems to be one of a kind where you have to be set up a pretty nifty algorithm taking care of all the different possible formats. That being said, there is no method which can parse ANY format you throw at it. you must write one yourself which knows which possible formats to expect.

              If its only those two formats you can expect:
              it should be an easy try-catch construct within a helper method. just be sure to use the right letters when setting up your SimpleDateFormat parse object, it's case sensitive and one quickly gets lost in all those letters.

              Again this should do the trick:
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");

              Just make sure you remove the last ":" from the JSONString (simple string manipulation) and then have two cases

              //removing last ":" from JSONString
              //Parsing string to date or gregCalendar
              GregorianCalendar time= new GregorianCalendar();
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
              SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");

              Edited by: Pascal B on 05.07.2012 04:50

              Edited by: Pascal B on 05.07.2012 04:55