Convert Date to XMLGregorianCalendar Object

7 years ago Lalit Bhagtani 0

Date to XMLGregorianCalendar Object :-

In this tutorial, we will learn to convert Date to XMLGregorianCalendar Object. If you had worked on consuming or publishing SOAP web services, then you would had faced the problem in converting Date to XMLGregorianCalendar Object and vice versa. Let’s see an implementation with the help of an example  :-

Example :- 

import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class DateToXMLGregorianCalendar {

  public static void main(String[] args) {
		
    /* Create Date Object */
    Date date = new Date();
		
    XMLGregorianCalendar xmlDate = null;
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);

    try{
      xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
    }catch(Exception e){
      e.printStackTrace();
    }
		
    System.out.println("XMLGregorianCalendar :- " + xmlDate);
		
  }	
	
}

Result :-

Date to XMLGregorianCalendar

You can also check :- 

Convert XMLGregorianCalendar to Date Object

Removing Timezone from XMLGregorianCalendar

That’s all for this post, If you liked it, please share your thoughts in comments section and share it with others too.