Re: dateTime
Pete Forman <[email protected]> 04 Nov 2004 13:03:39 +0000
| Newsgroups | gmane.text.xml.relaxng.general |
|---|---|
| Organization | WesternGeco, Gatwick, UK |
| Message-ID | <[email protected]> |
"Pawson, David" <[email protected]> writes: > http://www.w3.org/TR/xmlschema-2/#dateTime provides > examples such as > > 2003-05-31T13:20:05.50-05:00 > [...] > Can anyone help with the timezone formatting, to meet > the +-xx:nn format please? Here is some code I use in standard Java. You might also check out http://joda-time.sourceforge.net/ import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Now { public static void main(String[] args) { System.out.println(Now.getISO8601DateTime()); } public static String getISO8601DateTime() { SimpleDateFormat ISO8601Local = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss"); TimeZone timeZone = TimeZone.getDefault(); ISO8601Local.setTimeZone(timeZone); DecimalFormat twoDigits = new DecimalFormat("00"); Date now = new Date(); int offset = ISO8601Local.getTimeZone().getOffset(now.getTime()); String sign = "+"; if (offset < 0) { offset = -offset; sign = "-"; } int hours = offset / 3600000; int minutes = (offset - hours * 3600000) / 60000; // As things stand any odd seconds in the offset are silently truncated. // Uncomment the next 5 lines to detect that rare circumstance. //if (offset != hours * 3600000 + minutes * 60000) { // // E.g. TZ=Asia/Riyadh87 // throw new RuntimeException("TimeZone offset (" + sign + offset + // " ms) is not an exact number of minutes"); //} String ISO8601Now = ISO8601Local.format(now) + sign + twoDigits.format(hours) + ":" + twoDigits.format(minutes); return ISO8601Now; } } -- Pete Forman -./\.- Disclaimer: This post is originated WesternGeco -./\.- by myself and does not represent [email protected] -./\.- opinion of Schlumberger, Baker http://petef.port5.com -./\.- Hughes or their divisions.