You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2008/05/13 18:44:40 UTC

[digester] implementation of String->Date for xs:dateTime

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

All,

Hi, I'm looking for an implementation of a String->Date converter for
xs:dateTime, which has an odd format. It's not directly supportable by
java.text.SimpleDateFormat and looks like a regular expression is the
only way to go.

Does anyone have an implementation they'd care to share, or any tips in
particular? I'd be glad to share whatever I come up with if there isn't
anything already out there.

Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgpxXgACgkQ9CaO5/Lv0PDojwCgmWjCWbNWHyeYaVX9v/ARtwb/
ZeQAoMDbonmKKK8WQWvZai/Kr8+MaXpk
=dmkq
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [digester] implementation of String->Date for xs:dateTime

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Simon,

simon wrote:
| I just don't see it belonging in Digester either.
|
| Perhaps you could put this on the Digester wiki page. If you want to
| do this, I suggest creating a "TypeConverters" page, and linking to
| it from the main page or similar. And maybe linking to it from the
| BeanUtils page too.

That's a thought. Okay, I'll run it op the flag pole in beanutils and,
if they reject it, I'll put it into the wiki.

Thanks for the suggestions, guys.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkhitZ8ACgkQ9CaO5/Lv0PCDUACgh6BjMRz9A0nQFlBKG9y6Rubm
sYIAoJQt5rY3FMv6GqMja7l82GiBdkza
=qwv2
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [digester] implementation of String->Date for xs:dateTime

Posted by simon <si...@chello.at>.
On Wed, 2008-06-25 at 15:34 -0400, Rahul Akolkar wrote:
> On 6/25/08, Christopher Schultz <ch...@christopherschultz.net> wrote:
> > -----BEGIN PGP SIGNED MESSAGE-----
> >  Hash: SHA1
> >
> >  Simon,
> >
> >  Sorry for the late followup.
> >
> >  simon.kitching@chello.at wrote:
> >  | Christopher Schultz schrieb:
> >  |
> >  |> Certainly. Should I log it against digester (where it is most useful) or
> >  |> beanutils (where all the stock Converters actually live)?
> >  |
> >  | Not against Digester please. Digester doesn't do any data conversion
> >  | itself, just invokes BeanUtils.
> >  |
> >  | BeanUtils seems the appropriate place for this.
> >
> >  Since this feature primarily benefits users of Digester, and Digester
> >  already has a dependency on beanutils, why not put this
> >  digester-specific capability into the digester?
> >
> <snip/>
> 
> I don't see this as [digester]-specific, though your usecase might be
> tied to it. Converters are a better fit in [beanutils].

I just don't see it belonging in Digester either.

Perhaps you could put this on the Digester wiki page. If you want to do this, I suggest creating a "TypeConverters" page, and linking to it from the main page or similar. And maybe linking to it from the BeanUtils page too.

Regards,
Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [digester] implementation of String->Date for xs:dateTime

Posted by Rahul Akolkar <ra...@gmail.com>.
On 6/25/08, Christopher Schultz <ch...@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
>  Hash: SHA1
>
>  Simon,
>
>  Sorry for the late followup.
>
>  simon.kitching@chello.at wrote:
>  | Christopher Schultz schrieb:
>  |
>  |> Certainly. Should I log it against digester (where it is most useful) or
>  |> beanutils (where all the stock Converters actually live)?
>  |
>  | Not against Digester please. Digester doesn't do any data conversion
>  | itself, just invokes BeanUtils.
>  |
>  | BeanUtils seems the appropriate place for this.
>
>  Since this feature primarily benefits users of Digester, and Digester
>  already has a dependency on beanutils, why not put this
>  digester-specific capability into the digester?
>
<snip/>

I don't see this as [digester]-specific, though your usecase might be
tied to it. Converters are a better fit in [beanutils].

-Rahul


>  I'm not really objecting to putting this into beanutils, but I suspect
>  I'll get a "this doesn't belong here" response from the beanutils folks. ;)
>
>  Thanks,
>  - -chris

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [digester] implementation of String->Date for xs:dateTime

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Simon,

Sorry for the late followup.

simon.kitching@chello.at wrote:
| Christopher Schultz schrieb:
|
|> Certainly. Should I log it against digester (where it is most useful) or
|> beanutils (where all the stock Converters actually live)?
|
| Not against Digester please. Digester doesn't do any data conversion
| itself, just invokes BeanUtils.
|
| BeanUtils seems the appropriate place for this.

Since this feature primarily benefits users of Digester, and Digester
already has a dependency on beanutils, why not put this
digester-specific capability into the digester?

I'm not really objecting to putting this into beanutils, but I suspect
I'll get a "this doesn't belong here" response from the beanutils folks. ;)

Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkhiU9cACgkQ9CaO5/Lv0PBzIACgrDoNFsI6jmbAnw82kwuB5ij2
oQ4AnRlTOpppa1ol4w+/YPYGrvDkz9jA
=OGmF
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [digester] implementation of String->Date for xs:dateTime

Posted by "simon.kitching@chello.at" <si...@chello.at>.
Christopher Schultz schrieb:
> Sebb,
>
> sebb wrote:
> |>  Here is the implementation that I developed [for an xs:dateTime
> converter]. Oddly,
> |>  it seems that sometimes the TimeZone is incorrectly set (though the
> |>  normalized time appears to be correct) in the resulting Date. Any tips
> |>  would be appreciated.
> |>
> |>  Apologies for the poor formatting.
> |>
> |
> | Could you please create a JIRA enhancement issue and attach the code
> there?
>
> Certainly. Should I log it against digester (where it is most useful) or
> beanutils (where all the stock Converters actually live)?

Not against Digester please. Digester doesn't do any data conversion
itself, just invokes BeanUtils.

BeanUtils seems the appropriate place for this.

Regards, Simon


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [digester] implementation of String->Date for xs:dateTime

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sebb,

sebb wrote:
|>  Here is the implementation that I developed [for an xs:dateTime
converter]. Oddly,
|>  it seems that sometimes the TimeZone is incorrectly set (though the
|>  normalized time appears to be correct) in the resulting Date. Any tips
|>  would be appreciated.
|>
|>  Apologies for the poor formatting.
|>
|
| Could you please create a JIRA enhancement issue and attach the code
there?

Certainly. Should I log it against digester (where it is most useful) or
beanutils (where all the stock Converters actually live)?

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgy39EACgkQ9CaO5/Lv0PDeJQCeMPA722p+XE8TeOl+jHD8Rn6w
k00AniC00o2x2g1i0LsG00b/kUUcMJqp
=G/yZ
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [digester] implementation of String->Date for xs:dateTime

Posted by sebb <se...@gmail.com>.
On 19/05/2008, Christopher Schultz <ch...@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
>  Hash: SHA1
>
>  All,
>
>  Christopher Schultz wrote:
>  | Hi, I'm looking for an implementation of a String->Date converter for
>  | xs:dateTime, which has an odd format. It's not directly supportable by
>  | java.text.SimpleDateFormat and looks like a regular expression is the
>  | only way to go.
>  |
>  | Does anyone have an implementation they'd care to share, or any tips in
>  | particular? I'd be glad to share whatever I come up with if there isn't
>  | anything already out there.
>
>  As I received no useful replies, I'll assume that there is still a need
>  for such capability. Here is the implementation that I developed. Oddly,
>  it seems that sometimes the TimeZone is incorrectly set (though the
>  normalized time appears to be correct) in the resulting Date. Any tips
>  would be appreciated.
>
>  Apologies for the poor formatting.
>

Could you please create a JIRA enhancement issue and attach the code there?

Much easier to keep track of (and no formatting issues!).

Thanks!.

>  ======================
>
>  public class XMLDateConverter
>  ~    implements Converter
>  {
>  ~    private Converter _default;
>
>  ~    public XMLDateConverter(Converter defaultConverter)
>  ~    {
>  ~        _default = defaultConverter;
>  ~    }
>
>  ~        public Object convert(Class type, Object value)
>  ~            throws ConversionException
>  ~        {
>  ~            if(type.isAssignableFrom(Date.class)
>  ~               && value instanceof String)
>  ~            {
>  ~                try
>  ~                {
>  ~                    return
> parseXMLDateTime((String)value);
>  ~                }
>  ~                catch (ParseException pe)
>  ~                {
>  ~                    throw new ConversionException("Cannot parse date",
> pe);
>  ~                }
>  ~            }
>  ~            else if(null != _default)
>  ~            {
>  ~                return _default.convert(type, value);
>  ~            }
>  ~            else
>  ~            {
>  ~                throw new ConversionException("Don't know how to convert
>  " + value + " (" + (null == value ? "null" : value.getClass().getName())
>  + ") to " + type.getName());
>  ~            }
>  ~        }
>
>  ~    private static final Pattern XS_DATETIME_PATTERN =
>  Pattern.compile("([-]?[0-
> 9]{4,})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(.([0-9]{1,3}))?((
>  ?:[+-]?[0-9]{2}:[0-9]{2})|Z)?");
>
>  ~    private static Date parseXMLDateTime_regexp(String dateTime)
>  ~        throws ParseException
>  ~    {
>  ~        Matcher m =
> XS_DATETIME_PATTERN.matcher(dateTime);
>
>  ~        if(!m.matches())
>  ~            throw new ParseException("Unparsable date: " + dateTime, -1);
>
>  ~        Calendar date = Calendar.getInstance();
>
>  ~        date.set(Calendar.YEAR, Integer.parseInt(m.group(1)));
>  ~        date.set(Calendar.MONTH, Integer.parseInt(m.group(2)) - 1); //
>  Stupid!
>  ~        date.set(Calendar.DATE, Integer.parseInt(m.group(3)));
>
>  ~        date.set(Calendar.HOUR_OF_DAY, Integer.parseInt(m.group(4)));
>  ~        date.set(Calendar.MINUTE, Integer.parseInt(m.group(5)));
>  ~        date.set(Calendar.SECOND, Integer.parseInt(m.group(6)));
>
>  ~        if(null == m.group(7))
>  ~            date.set(Calendar.MILLISECOND, 0);
>  ~        else
>  ~            date.set(Calendar.MILLISECOND, Integer.parseInt(m.group(8)));
>
>  ~        int zoneOffset;
>  ~        String timezone = m.group(9);
>  ~        if(null == timezone
>  ~           || "Z".equals(timezone))
>  ~        {
>  ~            zoneOffset = 0;
>  ~        }
>  ~        else
>  ~        {
>  ~            int pos = timezone.indexOf(':');
>
>  ~            zoneOffset = ((60 *
> Integer.parseInt(timezone.substring(1,
>  pos)))
>  ~                          +
> Integer.parseInt(timezone.substring(pos + 1)))
>  ~                * 60 * 1000
>  ~                ;
>
>  ~            if(timezone.startsWith("-"))
>  ~                zoneOffset = -zoneOffset;
>  ~        }
>
>  ~        date.set(Calendar.ZONE_OFFSET, zoneOffset);
>
>  ~        return date.getTime();
>  ~    }
>  }
>
>  - -chris
>  -----BEGIN PGP SIGNATURE-----
>  Version: GnuPG v1.4.9 (MingW32)
>  Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iEYEARECAAYFAkgxsHMACgkQ9CaO5/Lv0PDZrQCeMpFaaP3tfhNgtCjn5Qi/+8t/
>  OcIAni0ot3GbZfaM4NFzbFQCjr5JzHwW
>  =zDdg
>
>  -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
>  To unsubscribe, e-mail:
> user-unsubscribe@commons.apache.org
>  For additional commands, e-mail: user-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [digester] implementation of String->Date for xs:dateTime

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

All,

Christopher Schultz wrote:
| Hi, I'm looking for an implementation of a String->Date converter for
| xs:dateTime, which has an odd format. It's not directly supportable by
| java.text.SimpleDateFormat and looks like a regular expression is the
| only way to go.
|
| Does anyone have an implementation they'd care to share, or any tips in
| particular? I'd be glad to share whatever I come up with if there isn't
| anything already out there.

As I received no useful replies, I'll assume that there is still a need
for such capability. Here is the implementation that I developed. Oddly,
it seems that sometimes the TimeZone is incorrectly set (though the
normalized time appears to be correct) in the resulting Date. Any tips
would be appreciated.

Apologies for the poor formatting.

======================

public class XMLDateConverter
~    implements Converter
{
~    private Converter _default;

~    public XMLDateConverter(Converter defaultConverter)
~    {
~        _default = defaultConverter;
~    }

~        public Object convert(Class type, Object value)
~            throws ConversionException
~        {
~            if(type.isAssignableFrom(Date.class)
~               && value instanceof String)
~            {
~                try
~                {
~                    return parseXMLDateTime((String)value);
~                }
~                catch (ParseException pe)
~                {
~                    throw new ConversionException("Cannot parse date", pe);
~                }
~            }
~            else if(null != _default)
~            {
~                return _default.convert(type, value);
~            }
~            else
~            {
~                throw new ConversionException("Don't know how to convert
" + value + " (" + (null == value ? "null" : value.getClass().getName())
+ ") to " + type.getName());
~            }
~        }

~    private static final Pattern XS_DATETIME_PATTERN =
Pattern.compile("([-]?[0-
9]{4,})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(.([0-9]{1,3}))?((
?:[+-]?[0-9]{2}:[0-9]{2})|Z)?");

~    private static Date parseXMLDateTime_regexp(String dateTime)
~        throws ParseException
~    {
~        Matcher m = XS_DATETIME_PATTERN.matcher(dateTime);

~        if(!m.matches())
~            throw new ParseException("Unparsable date: " + dateTime, -1);

~        Calendar date = Calendar.getInstance();

~        date.set(Calendar.YEAR, Integer.parseInt(m.group(1)));
~        date.set(Calendar.MONTH, Integer.parseInt(m.group(2)) - 1); //
Stupid!
~        date.set(Calendar.DATE, Integer.parseInt(m.group(3)));

~        date.set(Calendar.HOUR_OF_DAY, Integer.parseInt(m.group(4)));
~        date.set(Calendar.MINUTE, Integer.parseInt(m.group(5)));
~        date.set(Calendar.SECOND, Integer.parseInt(m.group(6)));

~        if(null == m.group(7))
~            date.set(Calendar.MILLISECOND, 0);
~        else
~            date.set(Calendar.MILLISECOND, Integer.parseInt(m.group(8)));

~        int zoneOffset;
~        String timezone = m.group(9);
~        if(null == timezone
~           || "Z".equals(timezone))
~        {
~            zoneOffset = 0;
~        }
~        else
~        {
~            int pos = timezone.indexOf(':');

~            zoneOffset = ((60 * Integer.parseInt(timezone.substring(1,
pos)))
~                          + Integer.parseInt(timezone.substring(pos + 1)))
~                * 60 * 1000
~                ;

~            if(timezone.startsWith("-"))
~                zoneOffset = -zoneOffset;
~        }

~        date.set(Calendar.ZONE_OFFSET, zoneOffset);

~        return date.getTime();
~    }
}

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgxsHMACgkQ9CaO5/Lv0PDZrQCeMpFaaP3tfhNgtCjn5Qi/+8t/
OcIAni0ot3GbZfaM4NFzbFQCjr5JzHwW
=zDdg
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [digester] implementation of String->Date for xs:dateTime

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sebb,

sebb wrote:
| What is "xs:dateTime" format?
|
| Where it is defined/documented?

http://www.w3.org/TR/xmlschema-2/#dateTime

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgsXZ4ACgkQ9CaO5/Lv0PDtUQCfSh9tA51UI90mhjDKBFZqjI/x
g3wAnAsH/WDGOXwwkYVXa/INgwAOgVuq
=E8tl
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [digester] implementation of String->Date for xs:dateTime

Posted by sebb <se...@gmail.com>.
On 15/05/2008, Christopher Schultz <ch...@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
>  Hash: SHA1
>
>  All,
>
>  Anyone?
>

What is "xs:dateTime" format?

Where it is defined/documented?

>  - -chris
>
>  Christopher Schultz wrote:
>  | All,
>  |
>  | Hi, I'm looking for an implementation of a String->Date converter for
>  | xs:dateTime, which has an odd format. It's not directly supportable by
>  | java.text.SimpleDateFormat and looks like a regular expression is the
>  | only way to go.
>  |
>  | Does anyone have an implementation they'd care to share, or any tips in
>  | particular? I'd be glad to share whatever I come up with if there isn't
>  | anything already out there.
>  |
>  | Thanks,
>  | -chris
>
>  -
> ---------------------------------------------------------------------
>  To unsubscribe, e-mail:
> user-unsubscribe@commons.apache.org
>  For additional commands, e-mail: user-help@commons.apache.org
>
>  -----BEGIN PGP SIGNATURE-----
>  Version: GnuPG v1.4.9 (MingW32)
>  Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iEYEARECAAYFAkgsVU0ACgkQ9CaO5/Lv0PAOhACgmd9gHebZDAkQvVDcWMxff9s7
>  XIEAnRuKr16ssG8wEO2EKOE5b1+uZqR8
>  =cUJR
>
>  -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
>  To unsubscribe, e-mail:
> user-unsubscribe@commons.apache.org
>  For additional commands, e-mail: user-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [digester] implementation of String->Date for xs:dateTime

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

All,

Anyone?

- -chris

Christopher Schultz wrote:
| All,
|
| Hi, I'm looking for an implementation of a String->Date converter for
| xs:dateTime, which has an odd format. It's not directly supportable by
| java.text.SimpleDateFormat and looks like a regular expression is the
| only way to go.
|
| Does anyone have an implementation they'd care to share, or any tips in
| particular? I'd be glad to share whatever I come up with if there isn't
| anything already out there.
|
| Thanks,
| -chris

- ---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgsVU0ACgkQ9CaO5/Lv0PAOhACgmd9gHebZDAkQvVDcWMxff9s7
XIEAnRuKr16ssG8wEO2EKOE5b1+uZqR8
=cUJR
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org