首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

[转贴]XMLGregorianCalendar门类和Date类型之间的相互转换

2012-09-19 
[转贴]XMLGregorianCalendar类型和Date类型之间的相互转换import java.text.SimpleDateFormatimport java

[转贴]XMLGregorianCalendar类型和Date类型之间的相互转换

import java.text.SimpleDateFormat;import java.util.Date;import java.util.GregorianCalendar;import javax.xml.datatype.DatatypeFactory;import javax.xml.datatype.XMLGregorianCalendar;/** * XMLGregorianCalendar类型和Date类型之间的相互转换 * @author Xin * 2010-06-12 */public class DateTest {    public XMLGregorianCalendar convertToXMLGregorianCalendar(Date date) {        GregorianCalendar cal = new GregorianCalendar();        cal.setTime(date);        XMLGregorianCalendar gc = null;        try {            gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);        } catch (Exception e) {             e.printStackTrace();        }        return gc;    }      public  Date convertToDate(XMLGregorianCalendar cal) throws Exception{         GregorianCalendar ca = cal.toGregorianCalendar();         return ca.getTime();     }      public static void main(String[] args) {         DateTest dateTest = new DateTest();         XMLGregorianCalendar d = dateTest.convertToXMLGregorianCalendar(new Date());         System.out.println(d.getDay());         XMLGregorianCalendar cal = null;         try {             cal = DatatypeFactory.newInstance().newXMLGregorianCalendar();             cal.setMonth(06);             cal.setYear(2010);              Date date = dateTest.convertToDate(cal);             String format = "yyyy-MM-dd HH:mm:ss";             SimpleDateFormat formatter = new SimpleDateFormat(format);             System.out.println(formatter.format(date));         } catch (Exception e) {             e.printStackTrace();         }     }}



http://blog.csdn.net/Jamse228/archive/2010/06/12/5666689.aspx

热点排行