How to Convert Java Date to XML DateTime String?
Last Updated :
04 Jul, 2022
In order to define a date and time, the DateTime data type is used. DateTime is defined in the format as “YYYY-MM-DDThh:mm:ss” where:
- YYYY states the year
- MM represents the month
- DD shows the day
- T indicates the beginning of the time segment needed.
- Hh determines the hour
- mm represents the minute
- ss indicates the second
Example: 2002-05-30T09:00:00
What are Time Zones in XML DateTime format?
In order to specify a time zone, we can either enter a DateTime in UTC time by inserting a "Z" behind the time,
Example:
2002-05-30T09:30:10Z
Or we can determine an offset from the UTC time by adding a positive or negative time behind the time,
Example:
2002-05-30T09:30:10-06:00
2002-05-30T09:30:10+06:00
So, The timezone may be defined as "Z" (UTC) or "(+|-)hh:mm." Undefined timezones are called "undetermined." The literal “Z”(Zulu) is used as a time-zone indicator, which indicates that the time is UTC when added at the end of a time.
What is Time Offset?
A time offset is an amount of time to be added or subtracted from the Coordinated Universal Time (UTC) time to get the current time of a specific place.
Approach to convert Java Date to XML DateTime String:
- Firstly we create an object of SimpleDateFormat. This class parses and formats the date and time in Java.
- Then, we create a StringBuffer which will hold the XML formatted string.
- Further, we calculate the ZoneOffset. It determines a time zone offset from Greenwich/UTC time. A time-zone offset is the amount of time that a time-zone differs from Greenwich/UTC. This is usually a fixed number of hours and minutes. Different parts of the world have different time-zone offsets. For example, India is 05:30 ahead of the Greenwich/UTC.
- At last, we combine all the required information in a single string, which is the formatted XML string.
Java
// Java program to Convert Java Date to XML DateTime String
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class GFG {
public static void main(String[] args)
{
// formatting time
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("HH:mm:ss");
// create a StringBuffer(in order to use its append
// functionality) to store the date in XML DateTime
// format
StringBuffer buff = new StringBuffer();
// get the date of the system by creating an
// instance of the Date class
Date date = new Date();
// append the formatted date(yyyy-MM-dd) in the
// buffer
buff.append(format1.format(date));
// append T
buff.append('T');
// and finally append the formatted time(HH:mm:ss) in
// buffer
buff.append(format2.format(date));
// calculating time zone
// get the calendar instance in order to get the
// time offset
Calendar calendar = Calendar.getInstance();
// The get(int field_value) method of Calendar class
// is used to return the value of the given calendar
// field in the parameter.
int offset = calendar.get(calendar.ZONE_OFFSET)
/ (1000 * 60);
// add the sign(+/-) according to the value of the
// offset
if (offset < 0) {
buff.append('-');
// if the offset is negative make it positive by
// multiplying it with -1, we will be using it
//further
offset *= -1;
}
else {
buff.append('+');
}
// get the hour from the offset and store it in a
// String
String s1 = String.valueOf(offset / 60);
// check if the retrieved hour is single digit or
// two digit in case of single digit, add 0 before
// the significant value
for (int i = s1.length(); i < 2; i++) {
buff.append('0');
}
// then finally append the s1 in our buffer
buff.append(s1);
buff.append(':');
// now retrieve the minutes from offset, and
// validate it in the same way as we did for the hour
String s2 = String.valueOf(offset % 60);
for (int i = s2.length(); i < 2; i++) {
buff.append('0');
}
// append the minutes in buffer
buff.append(s2);
// finally we are done formatting the Java Date time
// into XML DateTime format convert the buffer into
// the String, and print it
System.out.println(buff.toString());
}
}
Output2021-02-23T10:38:30+00:00