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
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class GFG {
public static void main(String[] args)
{
SimpleDateFormat format1 = new SimpleDateFormat( "yyyy-MM-dd" );
SimpleDateFormat format2 = new SimpleDateFormat( "HH:mm:ss" );
StringBuffer buff = new StringBuffer();
Date date = new Date();
buff.append(format1.format(date));
buff.append( 'T' );
buff.append(format2.format(date));
Calendar calendar = Calendar.getInstance();
int offset = calendar.get(calendar.ZONE_OFFSET)
/ ( 1000 * 60 );
if (offset < 0 ) {
buff.append( '-' );
offset *= - 1 ;
}
else {
buff.append( '+' );
}
String s1 = String.valueOf(offset / 60 );
for ( int i = s1.length(); i < 2 ; i++) {
buff.append( '0' );
}
buff.append(s1);
buff.append( ':' );
String s2 = String.valueOf(offset % 60 );
for ( int i = s2.length(); i < 2 ; i++) {
buff.append( '0' );
}
buff.append(s2);
System.out.println(buff.toString());
}
}
|
Output
2021-02-23T10:38:30+00:00
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Jul, 2022
Like Article
Save Article