Format SOAP Date in Java

October 18, 2010

I needed to manually generate some XML containing a Date, from Java. Here's the code I needed to format the date correctly for a SOAP Message:

String lFormatTemplate = "yyyy-MM-dd'T'hh:mm:ss'Z'";
DateFormat lDateFormat = new SimpleDateFormat(lFormatTemplate);
String lDate = lDateFormat.format(lDateToReturn);

return lDate;

To convert back from a string to a date:

DateFormat lDateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
Date lSystemDateTime = null;

try
{
   lSystemDateTime = lDateFormatter.parse(lDate);
}
catch (ParseException lEx)
{
  throw new RuntimeException("Can't parse " + lDate + " as process date");
}