原文:http://xiyangzk.iteye.com/blog/813936
XMLGregorianCalendar是GregorianCalendar(格里历,阳历)在XML领域中的对应类,算是Webservice、JAXB这些应用经常出现的关于日期时间的经典入门问题。
使用javax.xml.datatype.DatatypeFactory、java.text.SimpleDateFormat、java.util.GregorianCalendar可以轻松解决XMLGregorianCalendar相关问题。
-
- GregorianCalendar nowGregorianCalendar =new GregorianCalendar();
- XMLGregorianCalendar xmlDatetime= DatatypeFactory.newInstance().newXMLGregorianCalendar(nowGregorianCalendar);
-
- nowGregorianCalendar = xmlDatetime.toGregorianCalendar();
-
- SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateTimeString=simpleDateFormat.format(nowGregorianCalendar.getTime());
-
- Date date = nowGregorianCalendar.getTime();
-
- nowGregorianCalendar.setTime(date);
-
-
- date=simpleDateFormat.parse("1981-07-22 00:00:00");
-
- String dateString=simpleDateFormat.format(date.getTime());
-
-
- nowGregorianCalendar.setTime(simpleDateFormat.parse("1981-07-22 00:00:00"));
- xmlDatetime= DatatypeFactory.newInstance().newXMLGregorianCalendar(nowGregorianCalendar);
-
- simpleDateFormat.format(xmlDatetime.toGregorianCalendar().getTime());
-
- GregorianCalendar nowGregorianCalendar =new GregorianCalendar();
- XMLGregorianCalendar xmlDatetime= DatatypeFactory.newInstance().newXMLGregorianCalendar(nowGregorianCalendar);
-
- nowGregorianCalendar = xmlDatetime.toGregorianCalendar();
-
- SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateTimeString=simpleDateFormat.format(nowGregorianCalendar.getTime());
-
- Date date = nowGregorianCalendar.getTime();
-
- nowGregorianCalendar.setTime(date);
-
-
- date=simpleDateFormat.parse("1981-07-22 00:00:00");
-
- String dateString=simpleDateFormat.format(date.getTime());
-
-
- nowGregorianCalendar.setTime(simpleDateFormat.parse("1981-07-22 00:00:00"));
- xmlDatetime= DatatypeFactory.newInstance().newXMLGregorianCalendar(nowGregorianCalendar);
-
- simpleDateFormat.format(xmlDatetime.toGregorianCalendar().getTime());
//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());
附:java.util.GregorianCalendar与java.util.Date的关系如同日历与日历中的某一页。java.util.GregorianCalendar与.Net类型DateTime更相像。