commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: [digester] implementation of String->Date for xs:dateTime
Date Mon, 19 May 2008 16:53:07 GMT
-----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


Mime
View raw message