Reputation: 989
I have code snippet which is giving different output in scala and java. I want the same output as in Java, any one please guide.
Output in Java: 2012-12-13T10:36:38
Output in 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());
}
}
Upvotes: 1
Views: 1800
Reputation: 989
I have solved the problem please find the solution below.
I have set the timezone and millisecond to undefined in the above code.
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())
}
Upvotes: 0
Reputation: 1703
Maybe you should change a timezone
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
Result:
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]
Upvotes: 1