I need to set some XML Schema Date/Time Datatypes (also called xsd date) values for Created (being now) and Expired (one month from now) so that the result might look something like a dateTime in UTC (also known as GMT, but also being a 24 hour clock format) time by adding a “Z” (for Zulu?) behind the time
with much chin pulling and Google cursing (suffering so you don’t have to) I came up with this :
import java.util.*;
import java.text.SimpleDateFormat;
public class xsdDate {
private static final SimpleDateFormat SDF =
new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
public String getCreatedxsdDate () {
GregorianCalendar cal = new GregorianCalendar();
final TimeZone utc = TimeZone.getTimeZone( "UTC" );
SDF.setTimeZone( utc );
final String milliformat = SDF.format( new Date() );
final String zulu = milliformat.substring( 0, 22 ) + 'Z';
return zulu ;
}
public String getExpiresxsdDate () {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date());
cal.add(Calendar.MONTH, 1); // adjust date by one month
final TimeZone utc = TimeZone.getTimeZone( "UTC" );
SDF.setTimeZone( utc );
final String milliformat = SDF.format( cal.getTime() );
final String zulu = milliformat.substring( 0, 22 ) + 'Z';
return zulu ;
}
}
as an example of how to use it :
xsdDate d = new xsdDate();
System.out.println("CreatedxsdDate Date: " + d.getCreatedxsdDate ());
System.out.println("ExpiresxsdDate Date: " + d.getExpiresxsdDate ());
joy!