Java 8 – How to convert Timestamp to XMLGregorianCalendar and vice-versa ?

In this article, we will learn how to convert Timestamp to XMLGregorianCalendar and vice-versa in Java

Timestamp to XMLGregorianCalendar conversion & vice-versa :

It is very simple and easy to convert Timestamp to XMLGregorianCalendar and viceversa

  • Convert Timestamp to XMLGregorianCalendar
    • First, instantiate Timestamp
    • And then create XMLGregorianCalendar object
    • Now, extract each parts/fields like day, month, year, hour, minute, second and milli from Timestamp and set their values in XMLGregorianCalendar using their setter methods
  • Convert XMLGregorianCalendar to Timestamp
    • First, create XMLGregorianCalendar object
    • And then instantiate Timestamp and pass XMLGregorianCalendar object after converting into long using toGregorianCalendar() & getTimeInMillis() methods subsequently in Timestamp constructor

1. Convert Timestamp to XMLGregorianCalendar :

  • Instantiate Timestamp class and pass current date/time in millisecond using System.currentTimeMillis() which prints current Timestamp (date/time) in (yyyy-MM-dd HH:mm:ss.nnn) format
  • For Timestamp to XMLGregorianCalendar conversion,
    • Instantiate XMLGregorianCalendar object
    • And then extract each parts/fields like day, month, year, hour, minute, second, milli from Timestamp and set these values into XMLGregorianCalendar object using their setter methods
  • Finally, print XMLGregorianCalendar/Date in different formats using DateFormat/SimpleDateFormat classes

ConvertJavaSqlTimestampToXmlGregorianCalendar.java

package in.bench.resources.java8.date.examples;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

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

public class ConvertJavaSqlTimestampToXmlGregorianCalendar {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws DatatypeConfigurationException {

		// 1. get Timestamp
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		System.out.println("Current Timestamp :- \n" + timestamp);


		// 2. get first 3 digits from Nanos part
		int milliFromNano = Integer.parseInt(
				String.valueOf(timestamp.getNanos())
				.substring(0, 3)
				);


		// 3. Conversion of Timestamp to XMLGregorianCalendar 
		XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
				.newInstance()
				.newXMLGregorianCalendar();
		xmlGregorianCalendar.setDay(timestamp.getDay());
		xmlGregorianCalendar.setMonth(timestamp.getMonth());
		xmlGregorianCalendar.setYear(timestamp.getYear());
		xmlGregorianCalendar.setHour(timestamp.getHours());
		xmlGregorianCalendar.setMinute(timestamp.getMinutes());
		xmlGregorianCalendar.setSecond(timestamp.getSeconds());
		xmlGregorianCalendar.setMillisecond(milliFromNano);
		System.out.println("\nConversion of Timestamp to XMLGregorianCalendar :- \n" 
				+ xmlGregorianCalendar);


		// 3.1 DateFormatter 1
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String formattedTimestampInStr1 = dateFormat.format(
				xmlGregorianCalendar
				.toGregorianCalendar()
				.getTime()
				);
		System.out.println("\nFormatted Date in (dd-MM-yyyy HH:mm:ss) format is :- \n" 
				+ formattedTimestampInStr1);


		// 3.2 DateFormatter 1
		dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm");
		String formattedTimestampInStr2 = dateFormat.format(
				xmlGregorianCalendar
				.toGregorianCalendar()
				.getTime()
				);
		System.out.println("\nFormatted Date in (dd/MMM/yy HH:mm) format is :- \n" 
				+ formattedTimestampInStr2);


		// 3.3 DateFormatter 1
		dateFormat = new SimpleDateFormat("EEEE dd MMMM, yyyy hh:mm:ss a z");
		String formattedTimestampInStr3 = dateFormat.format(
				xmlGregorianCalendar
				.toGregorianCalendar()
				.getTime()
				);
		System.out.print("\nFormatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- \n" 
				+ formattedTimestampInStr3);
	}
}

Output:

Current Timestamp :- 
2022-08-29 23:10:46.623

Conversion of Timestamp to XMLGregorianCalendar :- 
0122-07-01T23:10:46.623

Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :- 
02-07-0122 23:10:46

Formatted Date in (dd/MMM/yy HH:mm) format is :- 
02/Jul/22 23:10

Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Wednesday 02 July, 0122 11:10:46 pm IST

2. Convert XMLGregorianCalendar to Timestamp :

  • First, create XMLGregorianCalendar object
  • For XMLGregorianCalendar to Timestamp conversion,
    • Instantiate Timestamp object and pass XMLGregorianCalendar object after converting in long using toGregorianCalendar() & getTimeInMillis() methods in Timestamp’s constructor which will return Timestamp in (yyyy-MM-dd HH:mm:ss.nnn) format
  • Finally, print XMLGregorianCalendar/Date in different formats using DateFormat/SimpleDateFormat classes

ConvertXmlGregorianCalendarToJavaSqlTimestamp.java

package in.bench.resources.java8.date.examples;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

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

public class ConvertXmlGregorianCalendarToJavaSqlTimestamp {

	public static void main(String[] args) throws DatatypeConfigurationException {

		// 1. get new instance of XMLGregorianCalendar
		XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
				.newInstance()
				.newXMLGregorianCalendar(
						"2022-08-31T05:48:07.966+05:30"
						);
		System.out.println("XMLGregorianCalendar/Date is :- \n" + xmlGregorianCalendar);


		// 2. convert XMLGregorianCalendar to Timestamp
		Timestamp timestamp = new Timestamp(
				xmlGregorianCalendar
				.toGregorianCalendar()
				.getTimeInMillis()
				);
		System.out.println("\nConversion of XMLGregorianCalendar to Timestamp :- \n" 
				+ timestamp);


		// 3.1 DateFormatter 1
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String formattedTimestampInStr1 = dateFormat.format(timestamp);
		System.out.println("\nFormatted Date in (dd-MM-yyyy HH:mm:ss) format is :- \n" 
				+ formattedTimestampInStr1);


		// 3.2 DateFormatter 1
		dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm");
		String formattedTimestampInStr2 = dateFormat.format(timestamp);
		System.out.println("\nFormatted Date in (dd/MMM/yy HH:mm) format is :- \n" 
				+ formattedTimestampInStr2);


		// 3.3 DateFormatter 1
		dateFormat = new SimpleDateFormat("EEEE dd MMMM, yyyy hh:mm:ss a z");
		String formattedTimestampInStr3 = dateFormat.format(timestamp);
		System.out.print("\nFormatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- \n" 
				+ formattedTimestampInStr3);
	}
}

Output:

XMLGregorianCalendar/Date is :- 
2022-08-31T05:48:07.966+05:30

Conversion of XMLGregorianCalendar to Timestamp :- 
2022-08-31 05:48:07.966

Formatted Date in (dd-MM-yyyy HH:mm:ss) format is :- 
31-08-2022 05:48:07

Formatted Date in (dd/MMM/yy HH:mm) format is :- 
31/Aug/22 05:48

Formatted Date in (EEEE dd MMMM, yyyy hh:mm:ss a z) format is :- 
Wednesday 31 August, 2022 05:48:07 am IST

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert Calendar to GregorianCalendar and vice-versa ?
Java 8 – How to convert Timestamp to GregorianCalendar and vice-versa ?