Parsing of ISO8601 strings

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Parsing of ISO8601 strings

Bård Dybwad Kristensen
Hi

I use JodaTime to parse ISO8601 strings from web service xml.
If the iso8601 string is missing offset information, JodaTime by default uses the time zone that is default for the VM the code is running on.

Is there a way through JodaTime to verify that the iso8601 string actually has an offset defined so I can throw an Exception instead of JodaTime just going for the default time zone.

In other words, I would like to parse "2011-02-02T02:30:30.000+02:30" without any problem, but I would like to throw an exception if the string looks like "2011-02-02T02:30:30.000", instead of JodaTime parsing this using the server default time zone.

It is of course easy to write code for this, but I just wondered if there is something in JodaTime that could be used. 
The method
public static DateTimeFormatter forFields(
        Collection<DateTimeFieldType> fields,
        boolean extended,
        boolean strictISO)

looked promising (because of the strictIOS parameter), but there is no DateTimeFieldType for the offset field type.

regards,
Bård Dybwad Kristensen

------------------------------------------------------------------------------
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parsing of ISO8601 strings

jodastephen
The ISODateTimeFormat class contains lots of suitable formats, and the
source code shows how to tune them. Basically, the formatter supports
optional sections, and most standard parsers use them. For your use
case, you simply don't want anything to be optional:

Some example code to get you started:

      DateTimeFormatter f =
DateTimeFormat.forPattern("yyyy-MM-dd'T'hh:mm:ss.SSSZZ");
      System.out.println(f.parseDateTime("2011-02-02T02:30:30.000+02:30"));
      System.out.println(f.withOffsetParsed().parseDateTime("2011-02-02T02:30:30.000+02:30"));
      System.out.println(f.parseDateTime("2011-02-02T02:30:30.000"));

thanks
Stephen


On 15 November 2011 16:16, Bård Dybwad Kristensen <[hidden email]> wrote:

> Hi
>
> I use JodaTime to parse ISO8601 strings from web service xml.
> If the iso8601 string is missing offset information, JodaTime by default
> uses the time zone that is default for the VM the code is running on.
>
> Is there a way through JodaTime to verify that the iso8601 string actually
> has an offset defined so I can throw an Exception instead of JodaTime just
> going for the default time zone.
>
> In other words, I would like to parse "2011-02-02T02:30:30.000+02:30"
> without any problem, but I would like to throw an exception if the string
> looks like "2011-02-02T02:30:30.000", instead of JodaTime parsing this using
> the server default time zone.
>
> It is of course easy to write code for this, but I just wondered if there is
> something in JodaTime that could be used.
> The method
> public static DateTimeFormatter forFields(
>         Collection<DateTimeFieldType> fields,
>         boolean extended,
>         boolean strictISO)
>
> looked promising (because of the strictIOS parameter), but there is
> no DateTimeFieldType for the offset field type.
>
> regards,
> Bård Dybwad Kristensen
>
> ------------------------------------------------------------------------------
> RSA(R) Conference 2012
> Save $700 by Nov 18
> Register now
> http://p.sf.net/sfu/rsa-sfdev2dev1
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>

------------------------------------------------------------------------------
Cloud Services Checklist: Pricing and Packaging Optimization
This white paper is intended to serve as a reference, checklist and point of
discussion for anyone considering optimizing the pricing and packaging model
of a cloud services business. Read Now!
http://www.accelacomm.com/jaw/sfnl/114/51491232/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...