|
[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Off topic: dateTime formatting in Java
Jochen Wiedmann <joe@i...> writes:
> [looking for Java to generate ]
> instances of xs:dateTime are formatted like
>
> 1999-05-31T13:20:00-05:00
My previous code fragment did UTC. This does local time as per the
original request.
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
Date now = new Date();
DateFormat ISO8601Local = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
TimeZone timeZone = TimeZone.getDefault();
ISO8601Local.setTimeZone(timeZone);
int offset = timeZone.getOffset(now.getTime());
String sign = "+";
if (offset < 0) {
offset = -offset;
sign = "-";
}
int hours = offset / 3600000;
int minutes = (offset - hours * 3600000) / 60000;
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");
}
DecimalFormat twoDigits = new DecimalFormat("00");
String ISO8601Now = ISO8601Local.format(now) + sign +
twoDigits.format(hours) + ":" + twoDigits.format(minutes);
--
Pete Forman -./\.- Disclaimer: This post is originated
WesternGeco -./\.- by myself and does not represent
pete.forman@w... -./\.- opinion of Schlumberger, Baker
http://petef.port5.com -./\.- Hughes or their divisions.
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|
|||||||||

Cart








