3 Replies Latest reply: Jul 5, 2012 6:55 AM by Pascal B RSS

    Parsing date from JSON response

    Chandresh
      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?

      Chandresh
        • 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 {
          datum.setTime(sdf.parse(JSONDate));
          } catch (ParseException e) {
          System.out.print("Error");
          System.out.println();
          }
          System.out.print(datum.toString());
          }

          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,
          Pascal
          • 2. Re: Parsing date from JSON response
            Chandresh
            I was seeing these two formats.

            2012-06-26T15:51:59.078-05:00
            2012-06-26T15:51:59-05:00

            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.

            2012-06-26T15:51:59.078Z
            2012-06-26T15:51:59Z

            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:
              http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat

              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:
              "
              2012-06-26T15:51:59.078-05:00
              2012-06-26T15:51:59-05:00
              "
              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.

              Edit:
              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();
              try{
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
              time.setTime(sdf.parse(JSONString));
              }
              catch{
              SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
              time.setTime(sdf2.parse(JSONString));
              }

              Edited by: Pascal B on 05.07.2012 04:50

              Edited by: Pascal B on 05.07.2012 04:55