当前位置:首页 > 开发 > 开源软件 > 正文

XMLGregorianCalendar与Date之间转换

发表于: 2015-07-20   作者:awen7916   来源:转载   浏览:
摘要: import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.XMLGregorianCalendar; /** *
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();
         }
     }
}

 

XMLGregorianCalendar与Date之间转换

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号