Java 怎么将date转为XMLGregorianCalendar

2019年7月21日19:38:02Java 怎么将date转为XMLGregorianCalendar已关闭评论 536 1047字阅读3分29秒

1.简介

Java 怎么将date转为XMLGregorianCalendar?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        Date date = new Date();
        System.out.println(dateToXMLGregorianCalendar(date));
    }

    public static XMLGregorianCalendar dateToXMLGregorianCalendar(Date date)
            throws ParseException, DatatypeConfigurationException {
        XMLGregorianCalendar result = null;
        GregorianCalendar gregorianCalendar;

        gregorianCalendar = (GregorianCalendar) GregorianCalendar
                .getInstance();
        gregorianCalendar.setTime(date);
        result = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(
                gregorianCalendar.get(Calendar.YEAR),
                gregorianCalendar.get(Calendar.MONTH) + 1,
                gregorianCalendar.get(Calendar.DAY_OF_MONTH),
                DatatypeConstants.FIELD_UNDEFINED);
        return result;
    }
}

 
继续阅读
编程十万个怎么办