尝试将XMLGregoriaCalnder转换为特定的时间段
基本上我通过XML从中国收到时间
<Date>2015-01-12</Date>
<Time>11:45:12</Time>
Unmarchall it
<Date>2015-01-12T11:45:12</Date>
现在我需要将它转换为纽约时间才能将其插入数据库
public static XMLGregorianCalendar getDate(final XMLGregorianCalendar date) {
TimeZone myzone = TimeZone.getTimeZone("America/New_York");
System.out.println(date + "..........");
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeZone(myzone);
calendar.set(date.getYear(), date.getMonth(), date.getDay(),
date.getHour(), date.getMinute(), date.getSecond());
XMLGregorianCalendar xmlGregorianCalendar = null;
if (date != null) {
try {
xmlGregorianCalendar = DatatypeFactory.newInstance()
.newXMLGregorianCalendar(calendar);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(xmlGregorianCalendar + "..........");
return xmlGregorianCalendar;
}
将输出:
2014-01-10T11:45:12..........
2014-02-10T11:45:12.063-05:00....................
我需要输出的内容:
2014-01-12T11:45:12..........
2014-01-11T21:45:12..........
任何帮助非常感谢
答案 0 :(得分:0)
原始时间与当地时间之间的时差是静态的,因此我尝试将时区差异(13小时)添加到原始时间,如下所示:
public XMLGregorianCalendar getDate(final XMLGregorianCalendar date, final XMLGregorianCalendar time) {
XMLGregorianCalendar date = null;
if (date != null && time != null) {
try {
newDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(date.getYear(), date.getMonth(), date.getDay(),
time.getHour(), time.getMinute(), time.getSecond(), time.getMillisecond(), 0);
Duration duration = DatatypeFactory.newInstance().newDurationDayTime(true, 0, 13, 0, 0);
newDate.add(duration);
}
catch (Exception e) {
LOGGER.error("Error creating date", e);
}
}
return newDate;