Parsing XML xs:date dates with and without explicit timezone

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

Parsing XML xs:date dates with and without explicit timezone

Morten Hattesen
Does Joda Time allow parsing XML Schema type "xs:date" dates with explicit time-zone specification, e.g. "2011-09-15Z" or "2011-09-15+02:00"?

I realize that this is a date format which is NOT supported by ISO-8601 (which would require a "T" to be specified, like "2011-09-15T+02:00".

An "unzoned" date may be parsed like so (assuming my local timezone is +02:00):

Parsing in local timezone:
org.joda.time.format.ISODateTimeFormat.date().parseDateTime("2011-09-15").toString();
(java.lang.String) 2011-09-15T00:00:00.000+02:00

Parsing in Zulu timezone:
org.joda.time.format.ISODateTimeFormat.localDateParser().parseDateTime("2011-09-15").toString();
(java.lang.String) 2011-09-15T00:00:00.000Z

But I would really like to be able to parse *any* valid xs:date, (e.g. "2011-09-15Z", "2011-09-15+02:00" and "2011-09-15") allowing me to decide which "default" timezone to use for "unzoned" dates.

I could solve the problem using java.util.XmlGregorianCalendar (assuming my local timezone is +02:00):

Parsing "unzoned" xs:date:
new org.joda.time.DateTime(
    javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar("2011-09-15").toGregorianCalendar()
).toString();
(java.lang.String) 2011-09-15T00:00:00.000+02:00

Parsing xs:date with explicit Zulu timezone:
new org.joda.time.DateTime(
    javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar("2011-09-15Z").toGregorianCalendar()
).toString();
(java.lang.String) 2011-09-15T00:00:00.000Z

Unfortunately, as the above example shows, XmlGregorianCalendar doesn't seem to allow me to specify the "default" timezone, it simply parses an "unzoned" xs:date in the local timezone defined by the runtime environment.


Any hints?


------------------------------------------------------------------------------
Doing More with Less: The Next Generation Virtual Desktop
What are the key obstacles that have prevented many mid-market businesses
from deploying virtual desktops?   How do next-generation virtual desktops
provide companies an easier-to-deploy, easier-to-manage and more affordable
virtual desktop model.http://www.accelacomm.com/jaw/sfnl/114/51426474/
_______________________________________________
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 XML xs:date dates with and without explicit timezone

jodastephen
Hi,
Limited web access here...

DateTimeFormatterBuilder has the necessary power to create a formatter
with optional offset. You will need to call withOffsetParsed() on the
resultant formatter too.
Stephen

On 15/09/2011, Morten Hattesen <[hidden email]> wrote:

> Does Joda Time allow parsing XML Schema type "xs:date" dates with explicit
> time-zone specification, e.g. "2011-09-15Z" or "2011-09-15+02:00"?
>
> I realize that this is a date format which is NOT supported by ISO-8601
> (which would require a "T" to be specified, like "2011-09-15T+02:00".
>
> An "unzoned" date may be parsed like so (assuming my local timezone is
> +02:00):
>
> Parsing in local timezone:
>
> org.joda.time.format.ISODateTimeFormat.date().parseDateTime("2011-09-15").toString();
> (java.lang.String) 2011-09-15T00:00:00.000+02:00
>
>
> Parsing in Zulu timezone:
>
> org.joda.time.format.ISODateTimeFormat.localDateParser().parseDateTime("2011-09-15").toString();
> (java.lang.String) 2011-09-15T00:00:00.000Z
>
>
> But I would really like to be able to parse *any* valid xs:date,
> (e.g. "2011-09-15Z", "2011-09-15+02:00" and "2011-09-15") allowing me to
> decide which "default" timezone to use for "unzoned" dates.
>
> I could solve the problem using java.util.XmlGregorianCalendar (assuming my
> local timezone is +02:00):
>
> Parsing "unzoned" xs:date:
>
> new org.joda.time.DateTime(
>
> javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar("2011-09-15").toGregorianCalendar()
> ).toString();
> (java.lang.String) 2011-09-15T00:00:00.000+02:00
>
>
> Parsing xs:date with explicit Zulu timezone:
>
> new org.joda.time.DateTime(
>
> javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar("2011-09-15Z").toGregorianCalendar()
> ).toString();
> (java.lang.String) 2011-09-15T00:00:00.000Z
>
>
> Unfortunately, as the above example shows, XmlGregorianCalendar doesn't seem
> to allow me to specify the "default" timezone, it simply parses an "unzoned"
> xs:date in the local timezone defined by the runtime environment.
>
>
> Any hints?
>

------------------------------------------------------------------------------
Doing More with Less: The Next Generation Virtual Desktop
What are the key obstacles that have prevented many mid-market businesses
from deploying virtual desktops?   How do next-generation virtual desktops
provide companies an easier-to-deploy, easier-to-manage and more affordable
virtual desktop model.http://www.accelacomm.com/jaw/sfnl/114/51426474/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...