Monday, June 15, 2015

Date conversions

XSL to transform a date and shift to NZST

 <!-- Input: Dates are received in two formats: 2015-01-01-08.00.00 and 2015-01-01T08:00:00.000Z. Both represent UTC times.
Output: Dates are formatted as: 2015-01-01-08.00.00, and are shifted to NZST.
-->
<xsl:template name="formatDate" match="stDt|enDt">
<xsl:variable name="dateTimeStr" select="./text()" />
<xsl:value-of select="format-dateTime(xs:dateTime(concat(substring($dateTimeStr,1,10), 'T', substring($dateTimeStr,12,2), ':', substring($dateTimeStr,15,2), ':', substring($dateTimeStr,18,2))) + xs:dayTimeDuration('PT12H'), '[Y0001]-[M01]-[D01]-[H01].[m01].[s01]')" />
</xsl:template>

Java to convert a date to its local time

/**
* Convert the given date to its local time; if the date is in winter it is returned in NSZT, otherwise in NZDT.
*
* Example: dateTimeNZST = 10am 01-July-2000, return = 10am 01-July-2000
* Example: dateTimeNZST = 10am 01-January-2000, return = 11am 01-Jan-2000
*
* @param dateTimeNZST
* @return
*/
private static DateTime convertToLocal(DateTime dateTimeNZST) {
// Convert to NZDT if necessary
if (TimeZone.getTimeZone("Pacific/Auckland").inDaylightTime(dateTimeNZST.toDate())) {
return dateTimeNZST.plusHours(1);
} else {
return dateTimeNZST;
}
}