i have a XML Jaxb class to set with XMLGregorianCalendar type. But we are supposed to set only year in this attribute.
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(new
GregorianCalendar());
xmlCal.setYear(2021);
xmlObject.setBuiltYear(xmlCal);
// xmlCal.getYear() will give me year but its type int , so setter method not accepting it. If
has to be of type XMLGregorianCalendar only with year.
If i set it like above its giving 2021-09-23T10:19:38.346-04:00 but i need only year with type XMLGregorianCalendar . how we can do that ?
CodePudding user response:
Use XMLGregorianCalendar#clear
before setting the year.
Demo:
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class Main {
public static void main(String[] args) throws DatatypeConfigurationException {
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
xmlCal.clear();
xmlCal.setYear(2021);
System.out.println(xmlCal);
}
}
Output:
2021