如何将字符串转换为 xml 公历日期 java
How to Convert string to xml gregorian calendar date java
我正在尝试将字符串转换为公历日期,但无法转换。因为,字符串有不同的格式。比如“2015-05-22T16:28:40.317-04:00”。我看过其他一些例子,但它们不是这种时间格式。
我正在使用如下内容:
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss-SS:zz").parse(sampleDate));
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar( cal);
我什至也这样试过:
gregory.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(sampleDate));
如果您查看 SimpleDateFormat
文档,您会发现格式模式中没有 T
。为了转义非模式字符,将它们包裹在单引号 '
中,如本例所示(取自文档):
"hh 'o''clock' a, zzzz" -> 12 o'clock PM, Pacific Daylight Time
我认为正确的格式应该是这样的:
String format = "yyyy-MM-dd'T'HH:mm:ss.SSSX";
// ^-^-----check these
// don't pay attention to the smiley generated above, they're arrows ;)
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new SimpleDateFormat(format).parse(sampleDate));
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar( cal);
这也有效
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar("2015-05-22T16:28:40.317-04:00");
GregorianCalendar gregorianCalendar = xmlGregorianCalendar.toGregorianCalendar();
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss-SS:zz");
//dateFormat.setTimeZone(TimeZone.getTimeZone());
Date inputDate = dateFormat.parse(inputDatetime);
GregorianCalendar c = new GregorianCalendar();
c.setTime(inputDate);
XMLGregorianCalendar outputDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
return outputDate;
} catch (ParseException | DatatypeConfigurationException e) {
log.error("exception: {}", e.getMessage());
return null;
}
我正在尝试将字符串转换为公历日期,但无法转换。因为,字符串有不同的格式。比如“2015-05-22T16:28:40.317-04:00”。我看过其他一些例子,但它们不是这种时间格式。
我正在使用如下内容:
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss-SS:zz").parse(sampleDate));
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar( cal);
我什至也这样试过:
gregory.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(sampleDate));
如果您查看 SimpleDateFormat
文档,您会发现格式模式中没有 T
。为了转义非模式字符,将它们包裹在单引号 '
中,如本例所示(取自文档):
"hh 'o''clock' a, zzzz" -> 12 o'clock PM, Pacific Daylight Time
我认为正确的格式应该是这样的:
String format = "yyyy-MM-dd'T'HH:mm:ss.SSSX";
// ^-^-----check these
// don't pay attention to the smiley generated above, they're arrows ;)
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new SimpleDateFormat(format).parse(sampleDate));
XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar( cal);
这也有效
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar("2015-05-22T16:28:40.317-04:00");
GregorianCalendar gregorianCalendar = xmlGregorianCalendar.toGregorianCalendar();
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss-SS:zz");
//dateFormat.setTimeZone(TimeZone.getTimeZone());
Date inputDate = dateFormat.parse(inputDatetime);
GregorianCalendar c = new GregorianCalendar();
c.setTime(inputDate);
XMLGregorianCalendar outputDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
return outputDate;
} catch (ParseException | DatatypeConfigurationException e) {
log.error("exception: {}", e.getMessage());
return null;
}