具有特定格式的 XMLGregorianCalendar 日期
我得到一个 Date 对象,我需要将其转换为 XMLGregorian Calendar 特定格式
I am getting a Date object, which i need to convert to XMLGregorian Calendar specific format
我尝试了以下方法
String formattedDate = sdf.format(categoryData.getBulkCollectionTime()); //yyyy-MM-dd HH:mm:ss
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(formattedDate);
dataListType.setTimestamp(xmlCal);
我遇到了一个例外,我肯定在这里做错了.但是我想把Date对象格式化成指定的格式,sdf.format就完美完成了.
I am getting an exception, for sure I am doing wrong here. But I want to format the Date object into specified format, which is done by sdf.format perfectly.
但是我如何创建相同的 XMLGregorianCalendar 对象(来自 formattedDate)?
But how do I create the XMLGregorianCalendar object for the same (from formattedDate)?
推荐答案
可以通过日期对象本身来实现:
You can do it by the date object itself:
String formattedDate = sdf.format(categoryData.getBulkCollectionTime()); //yyyy-MM-dd HH:mm:ss
convertStringToXmlGregorian(formattedDate);
public XMLGregorianCalendar convertStringToXmlGregorian(String dateString)
{
try {
Date date = sdf.parse(dateString);
GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
gc.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
} catch (ParseException e) {
// Optimize exception handling
System.out.print(e.getMessage());
return null;
}
}
相关文章