You are currently browsing the daily archive for October 11th, 2006.
1. XSD DateTime Data Type
The dateTime data type is used to specify a date and a time.
The dateTime is specified in the following form “YYYY-MM-DDThh:mm:ss” where:
* YYYY indicates the year
* MM indicates the month
* DD indicates the day
* T indicates the start of the required time section
* hh indicates the hour
* mm indicates the minute
* ss indicates the second
Note: All components are required!
The following is an example of a dateTime declaration in a schema:
<xs:element name=”startdate” type=”xs:dateTime”/>
An element in your document might look like this:
<startdate>2002-05-30T09:00:00</startdate>
Or it might look like this:
<startdate>2002-05-30T09:30:10.5</startdate>
2. Time Zones
To specify a time zone, you can either enter a dateTime in UTC time by adding a “Z” behind the time – like this:
<startdate>2002-05-30T09:30:10Z</startdate>
or you can specify an offset from the UTC time by adding a positive or negative time behind the time – like this:
<startdate>2002-05-30T09:30:10-06:00</startdate>
or
<startdate>2002-05-30T09:30:10+06:00</startdate>
3. Duration Data Type
The duration data type is used to specify a time interval.
The time interval is specified in the following form “PnYnMnDTnHnMnS” where:
* P indicates the period (required)
* nY indicates the number of years
* nM indicates the number of months
* nD indicates the number of days
* T indicates the start of a time section (required if you are going to specify hours, minutes, or seconds)
* nH indicates the number of hours
* nM indicates the number of minutes
* nS indicates the number of seconds
The following is an example of a duration declaration in a schema:
<xs:element name=”period” type=”xs:duration”/>
An element in your document might look like this:
<period>P5Y</period>
The example above indicates a period of five years.
Or it might look like this:
<period>P5Y2M10D</period>
The example above indicates a period of five years, two months, and 10 days.
Or it might look like this:
<period>P5Y2M10DT15H</period>
The example above indicates a period of five years, two months, 10 days, and 15 hours.
Or it might look like this:
<period>PT15H</period>
The example above indicates a period of 15 hours.
4. Negative Duration
To specify a negative duration, enter a minus sign before the P:
<period>-P10D</period>
The example above indicates a period of minus 10 days.
5. Example:
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class TimeZoneConverter {
public static void main(String[] args) {
System.out.println(TimeZoneConverter.getISO8601DateTime());
}
public static String getISO8601DateTime() {
SimpleDateFormat ISO8601Local = new SimpleDateFormat(“yyyy-MM-dd’T'HH:mm:ss”);
TimeZone timeZone = TimeZone.getDefault(); //local JVM time zone
ISO8601Local.setTimeZone(timeZone);
DecimalFormat twoDigits = new DecimalFormat(“00″);
Date now = new Date();
int offset = ISO8601Local.getTimeZone().getOffset(now.getTime());
String sign = “+”;
if (offset < 0) {
offset = -offset;
sign = “-”;
}
int hours = offset / 3600000;
int minutes = (offset – hours * 3600000) / 60000;
// As things stand any odd seconds in the offset are silently truncated.
// Uncomment the next 5 lines to detect that rare circumstance.
//if (offset != hours * 3600000 + minutes * 60000) {
// // E.g. TZ=Asia/Riyadh87
// throw new RuntimeException(“TimeZone offset (” + sign + offset +
// ” ms) is not an exact number of minutes”);
//}
String ISO8601Now = ISO8601Local.format(now) + sign +
twoDigits.format(hours) + “:” + twoDigits.format(minutes);
return ISO8601Now;
}
}





