Ldap date conversion

I export users from ldap programmatically. Therefore, I am extracting users from ldap. One of the attributes is whenCreated.

One of the values ​​that I need to convert: 20090813145607.0ZDirectly breaking it, I get the following format: yyyyMMddHHmmss+ .0Z. The problem is that the application runs in the CET time zone, and the storage time is UTC, which is probably indicated in .0Z. This 14:56 UTCis a local view 16:56. For summer time, it seems 2 hours and for winter time 1 hour.

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

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

Shows an invalid date because it ignores the time zone of dates.

Is there any way to directly convert it?

+5
source share
7 answers

How about using the splitting described above and then reformatting the time zone 0Zto standard format and then using sdf.parse(...)? Maybe something like this (with the addition of appropriate error checking, 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);
+5
source

Checking the RFC mentioned above seems to use UTC is the recommended default behavior for ldap dates. So I converted it directly:

public Date parseLdapDate(String ldapDate){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

    try {
        return sdf.parse(ldapDate);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
+6
source

ISO 8601

, RFC 4517 Lightweight Directory Access Protocol (LDAP): . . 3.3.13 " ".

, LDAP , ISO 8601. , , "" ISO 8601.

Z Zulu UTC ( , GMT).

. , () RFC 4517, ISO 8601. ISO 8601. RFC 4517 ( ) / . , : (a) ISO 8601 , (b) java.time .

java.time

java.time Java 8 . , java.util.Date, .Calendar java.text.SimpleDateFormat.

Joda-Time java.time.

, . Oracle. Qaru .

java.time Java 6 7 ThreeTen-Backport Android ThreeTenABP.

ThreeTen-Extra java.time . java.time.

RFC 4517. DateTimeFormatter . : uuuuMMddHHmmss[,S][.S]X. . , . . X Z, offset-from-UTC, -08 -0830, - 08:30 -083015 -08: 30: 15.

String input = "20090813145607.0Z";
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuuuMMddHHmmss[,S][.S]X" );
OffsetDateTime odt = OffsetDateTime.parse ( input , f );
Instant instant = odt.toInstant ();

.

System.out.println ( "input: " + input + " | odt: " + odt + " | instant: " + instant );

input: 20090813145607.0Z | odt: 2009-08-13T14: 56: 07Z | : 2009-08-13T14: 56: 07Z

, java.time.format.DateTimeParseException .

+4

. , , . whenCreated generalizedTime, . generalizedTime RFC4517.

+2

org.apache.directory.shared.ldap.util.DateUtils:

String ldapDate = "20090813145607.0Z";

date = DateUtils.parse(ldapDate);

generalizedTime = DateUtils.getGeneralizedTime(date);

+1

, :

static String parseLdapDate(String ldapDate) {

            long nanoseconds = Long.parseLong(ldapDate);   // nanoseconds since target time that you want to convert to java.util.Date

            long mills = (nanoseconds / 10000000);

            long unix = (((1970 - 1601) * 365) - 3 + Math.round((1970 - 1601) / 4)) * 86400L;

            long timeStamp = mills - unix;

            Date date = new Date(timeStamp * 1000L); // *1000 is to convert seconds to milliseconds
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); // the format of your date
    //sdf.setTimeZone(TimeZone.getTimeZone("GMT")); // give a timezone reference for formating (see comment at the bottom
            String formattedDate = sdf.format(date);

            return  formattedDate;
        }
+1

apache http://directory.apache.org/api/gen-docs/1.0.0-M11/apidocs/org/apache/directory/shared/util/GeneralizedTime.html

Direcotry:

GeneralizedTime gt = new GeneralizedTime(Calendar.getInstance());
String gtADString = gt.toGeneralizedTime(GeneralizedTime.Format.YEAR_MONTH_DAY_HOUR_MIN_SEC_FRACTION, GeneralizedTime.FractionDelimiter.DOT, 1, GeneralizedTime.TimeZoneFormat.Z).replaceFirst("Z", "\\.0Z");

, , . "1" , 3. "20120410011958.6Z" "20120410011958.687Z", ".0" Z. , ( , . AD )

GeneralizedTime gt = new GeneralizedTime(Calendar.getInstance());
String gtADString = gt.toGeneralizedTime(GeneralizedTime.Format.YEAR_MONTH_DAY_HOUR_MIN_SEC, GeneralizedTime.FractionDelimiter.DOT, 1, GeneralizedTime.TimeZoneFormat.Z).replaceFirst("Z", "\\.0Z");

By the way, this code is converted from a string format of a generic format AD to Java Date

 GeneralizedTime gt = new GeneralizedTime(str);
 Date d = gt.getCalendar().getTime();
0
source

All Articles