W3C Date and Time Formats
import java.util.GregorianCalendar;
import java.util.TimeZone;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class Main {
public static void main(String[] args)
throws DatatypeConfigurationException {
DatatypeFactory f = DatatypeFactory.newInstance();
// GregorianCalendar -> XMLGregorianCalendar
XMLGregorianCalendar c = f.newXMLGregorianCalendar(new GregorianCalendar());
// XMLGregorianCalendar -> XML dateTime
System.out.println(c.toXMLFormat());
// XML dateTime -> XMLGregorianCalendar
c = f.newXMLGregorianCalendar("2008-11-16T17:20:15.000+08:00");
System.out.println("getYear(): " + c.getYear());
c = f.newXMLGregorianCalendar();
c.setYear(2009);
c.setMonth(1);
c.setDay(17);
c.setTime(10, 15, 30);
c.setMillisecond(0);
TimeZone timezone = TimeZone.getDefault();
c.setTimezone(timezone.getRawOffset() / 60000);
System.out.println(c.toXMLFormat());
}
}
/*
2009-01-17T13:06:59.828+08:00
getYear(): 2008
2009-01-17T10:15:30.000+08:00
*/
沒有留言:
張貼留言