将 XMLGregorianCalender 转换为不同的时区
Convert XMLGregorianCalender to different timeZone
我正在尝试将 XMLGregoriaCalnder 转换为特定的时区
基本上我通过XML
收到来自中国的时间
<Date>2015-01-12</Date>
<Time>11:45:12</Time>
解开它
<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..........
非常感谢任何帮助
原始时间和本地时间之间的时差是静态的,所以我会尝试像这样简单地将时区差(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;
我正在尝试将 XMLGregoriaCalnder 转换为特定的时区
基本上我通过XML
收到来自中国的时间<Date>2015-01-12</Date>
<Time>11:45:12</Time>
解开它
<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..........
非常感谢任何帮助
原始时间和本地时间之间的时差是静态的,所以我会尝试像这样简单地将时区差(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;