The Sun BabelFish Blog
Don't panic !
SimpleDateFormat: A reason for more open source freedom with Java?
SimpleDateFormat implements rfc 822 dateTime time zone information. Sadly xml schema does this a slightly different way. The following template for example
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(date);
will produce a date like this
2005-06-12T10:32:00.000+0200
Whereas what I need for xml schema is
2005-06-12T10:32:00.000+02:00
It would be relatively easy to just make a copy of the SimpleDateFormat
class, rename it to XSDDateFormat and make a few
adjustments. Then I would have what I need. But the code is copyrighted
by IBM, so it is probably untoucheable. Clearly rewriting the class
itself and allowing it to keep its name would lead to a lot of
confusion, and should not be allowed. But using the code itself should
cause no one any harm, when clearly moved into a different class.
Of course this is not the whole story in the open source argument for Java, but it should be a consideration.
Update: I found XMLGregorianCalendar in the javax.xml.datatype package, and after some serious searching on the web, I found out how to use it. So here:
GregorianCalendar gc = new GregorianCalendar(); gc.setTimeInMillis(time.getTime()); DatatypeFactory df = DatatypeFactory.newInstance(); XMLGregorianCalendar xc = df.newXMLGregorianCalendar (gc); String ts = xc.toXMLFormat();
Kindof complicated really. But still, it works...
So what is the lesson now? That it is good that the code was not licenced liberally, because it forced me to find the right solution?
Posted at 12:56PM May 31, 2006 [permalink/trackback] by Henry Story in Java | Comments[1]
Note on comments:
- I know the forms below are a little small. We have asked for years for this to be changed, but I don't think it's going to happen soon. In Apple's Safari you can resize the entry box with you mouse. For people using other browsers click on this javascript link, that should allow you to resize your form.
- Comments are moderated, so they will take a little time to appear. Currently moderation means I have to read them personally. Hopefully with OpenId deployment, this will become more automated.
- HTML markup no longer works here, due to some decision made somewhere. Sorry about that.
- If you are having trouble posting, it may be that you need javascript to be enabled. I don't think javascript should be needed for submitting a form, but that's the way it is here.
- Check your comments by using the preview button...

I didnt want to simply use seconds since epoch and I wanted to be sure that come 2000 I could still sort numericly. Dealing with DBA's I frequently found files with DD-MM-YYYY or MM-DD-YYYY What I came up with was to use UTC as the time zone and YYYYMMDD.HHMMSS as the date.
20060531.113822
this allows for:
However this would encourage people to not use UTC and
would negate the ability to sort a directory of files numericly (sort -n)
expressed with the date command we get:
[7:41]Solaris(18)% /bin/date -u +%Y%m%d.%H%M%S
20060531.114123
the next logical step could be to use the gnu date builtin for UTC and iso-8601 timestamp:
[7:41am]Solaris(19)% /usr/local/gnu/bin/date -uIseconds
2006-05-31T11:41:24Z
and, for those who insist on a local time in their logs:
[7:41am]Solaris(20)% /usr/local/gnu/bin/date -Iseconds
2006-05-31T07:41:35-0400
this solves for local timezone usage in logfiles but still makes common storage break numerical sorting.
Posted by MikeTLive on May 31, 2006 at 02:07 PM CEST #