OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: Off topic: dateTime formatting in Java

[ Lists Home | Date Index | Thread Index ]

Jochen Wiedmann <joe@ispsoft.de> 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@westerngeco.com    -./\.-   opinion of Schlumberger, Baker
http://petef.port5.com           -./\.-   Hughes or their divisions.





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS