我有代码片段,它在scala和java中提供不同的输出。我想要与Java相同的输出,任何一个请指导。
Java输出:2012-12-13T10:36:38
Scala中的输出:2012-12-13T10:35:38.000+04:00
/**
* Convert the datetime to XMLGregorianCalendar datetime format.
* <br><h6>Example Date format You have to Give is : </h6>
* new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(Calendar.getInstance().getTime()))
* <br>1900-01-01T00:00:00
*/
public static XMLGregorianCalendar stringToXMLGregorianCalendar(String datetime) throws Exception {
try {
if(datetime == null || "".equals(datetime))
return null;
GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
gc.setTime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(datetime));
return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
} catch (DatatypeConfigurationException e) {
logger.error(e.fillInStackTrace());
throw new Exception(e.fillInStackTrace());
}
}
答案 0 :(得分:1)
也许你应该改变一个时区
val d = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(java.util.Calendar.getInstance().getTime())
val gc: java.util.GregorianCalendar = classOf[java.util.GregorianCalendar].newInstance().asInstanceOf[java.util.GregorianCalendar]; gc.setTime(new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(d));
gc.getTime
gc.getTimeZone
结果:
res6: java.util.Date = Thu Dec 13 17:21:50 GMT+02:00 2012
res7: java.util.TimeZone = sun.util.calendar.ZoneInfo[id="GMT+02:00",offset=7200000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
答案 1 :(得分:0)
我已经解决了问题,请找到下面的解决方案。
我在上面的代码中将时区和毫秒设置为未定义。
xgc.setMillisecond(DatatypeConstants.FIELD_UNDEFINED)
xgc.setTimezone(DatatypeConstants.FIELD_UNDEFINED)
try {
if (datetime == null || "".equals(datetime))
return null
val gc = new GregorianCalendar()
gc.setTime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(datetime))
val xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc)
xgc.setMillisecond(DatatypeConstants.FIELD_UNDEFINED)
xgc.setTimezone(DatatypeConstants.FIELD_UNDEFINED)
return xgc
} catch {
case e: DatatypeConfigurationException => logger.error(e.fillInStackTrace()); throw new Exception(e.fillInStackTrace())
}