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
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,
Or we can determine an offset from the UTC time by adding a positive or negative time behind the time,
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.