java.util.Date到XMLGregorianCalendar

没有从java.util.Date到XMLGregorianCalendar的简便方法吗?


GregorianCalendar c = new GregorianCalendar();
c.setTime(yourDate);
XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);

对于那些可能最终在这里寻找相反的转换(从XMLGregorianCalendarDate ):

XMLGregorianCalendar xcal = <assume this is initialized>;
java.util.Date dt = xcal.toGregorianCalendar().getTime();

这里是一个从GregorianCalendar转换为XMLGregorianCalendar的方法; 我将离开从java.util.Date转换为GregorianCalendar的部分,作为练习:

import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class DateTest {

   public static void main(final String[] args) throws Exception {
      GregorianCalendar gcal = new GregorianCalendar();
      XMLGregorianCalendar xgcal = DatatypeFactory.newInstance()
            .newXMLGregorianCalendar(gcal);
      System.out.println(xgcal);
   }

}

编辑:Slooow :-)

链接地址: http://www.djcxy.com/p/18669.html

上一篇: java.util.Date to XMLGregorianCalendar

下一篇: How to set time zone of a java.util.Date?