Udo Held Udo Held - 4 months ago 107
Java Question

Converting a ldap date

I'm exporting users from an ldap programmatically. Therefor I'm retrieving the users from ldap. One of the attributes is

whenCreated
.

One of the values I have to convert is:
20090813145607.0Z
Directly splitting it up I get the following format:
yyyyMMddHHmmss
+
.0Z
. The problem is that the application is running in CET timezone and the time stored is UTC which is probably indicated by the
.0Z
. It is
14:56 UTC
and the local representation is
16:56
. For summer time it seems to be 2 hours and for winter time 1 hour.

I checked the SimpleDateFormat and there is a placeholder for the timezone, however its a different format.

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
sdf.parse("20090813145607.0Z");


Will show the wrong date as it ignores the dates time zone.

Is there a way to convert it directly?

Answer

What about using the split you described above, then reformatting the 0Z timezone into a standard format, then using sdf.parse(...)? Maybe something like this (with appropriate error checking added, of course):

String[] parts = inputDateTime.split("[.]");
String dateTimePart = parts[0];
String timeZonePart = "+0" + parts[1].substring(0, parts[1].length() - 1) + "00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssZ");
Date theDate = sdf.parse(dateTimePart + timeZonePart);