javax.xml.datatype.XMLGregorianCalendar
XMLGregorianCalendar是GregorianCalendar(格里历,阳历)在XML领域中的对应类,算是Webservice、JAXB这些应用经常出现的关于日期时间的经典入门问题。
使用javax.xml.datatype.DatatypeFactory、java.text.SimpleDateFormat、java.util.GregorianCalendar可以轻松解决XMLGregorianCalendar相关问题。
- //GregorianCalendar-〉XMLGregorianCalendar
- GregorianCalendar nowGregorianCalendar =new GregorianCalendar();
- XMLGregorianCalendar xmlDatetime= DatatypeFactory.newInstance().newXMLGregorianCalendar(nowGregorianCalendar);
- //XMLGregorianCalendar ->GregorianCalendar
- nowGregorianCalendar = xmlDatetime.toGregorianCalendar();
- //GregorianCalendar->String
- SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateTimeString=simpleDateFormat.format(nowGregorianCalendar.getTime());
- //GregorianCalendar->Date
- Date date = nowGregorianCalendar.getTime();
- //Date->GregorianCalendar
- nowGregorianCalendar.setTime(date);
-
- //String->Date
- date=simpleDateFormat.parse("1981-07-22 00:00:00");
- //Date->String
- String dateString=simpleDateFormat.format(date.getTime());
-
- //Sring->XMLGregorianCalendar
- nowGregorianCalendar.setTime(simpleDateFormat.parse("1981-07-22 00:00:00"));
- xmlDatetime= DatatypeFactory.newInstance().newXMLGregorianCalendar(nowGregorianCalendar);
- //XMLGregorianCalendar->Sring
- simpleDateFormat.format(xmlDatetime.toGregorianCalendar().getTime());