I am trying to get the current date/time to populate in an XML. I am using the code
XMLGregorianCalendar xmlDate = null;
try {
xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(Calendar.YEAR, Calendar.MONTH+1, Calendar.DAY_OF_MONTH, Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND, DatatypeConstants.FIELD_UNDEFINED, TimeZone.LONG).normalize();
lastUpdatetDateTime.setTextContent(xmlDate.toXMLFormat());
} catch (DatatypeConfigurationException e) {
}
But I get the output as 0001-03-05T10:11:13Z, from all I know, we are in 2017! :)
Even the time is 8 minutes slower. I ran this at 11:21 AM CST.
Look at the arguments you're passing in to
newXMLGregorianCalendar
:The
Calendar.YEAR
,Calendar.MONTH
etc values are constants, used to refer to specific parts of a calendar. You'd typically use them like this:If you want to create an
XMLGregorianCalendar
for the current date/time, it looks like you need to do that explicitly:Better, use the
java.time
classes instead if you possibly can - thejava.util.Date
andjava.util.Calendar
classes are really nasty compared with eitherjava.time
or its backport.