如何将日历日期转换为 ldap 日期

How to convert calendar date to ldap date

目前我已经尝试过这段代码,但问题是第一行代码给出了这个输出 20150217172651+0100 而我需要的输出是 20150217172651.0Z

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");

运行 你的代码

public class poc {
    public static void main(String[] args) {
        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");
        System.out.println(gtADString);

    }
}

并将其作为输出:

20150217163832.0Z

使用以下依赖项:

<dependency>
    <groupId>org.apache.directory.api</groupId>
    <artifactId>api-util</artifactId>
    <version>1.0.0-M28</version>
</dependency>`

您可以尝试如下操作:

long t1 = calenderObj.getTimeInMillis();
calenderObj.set(1601, 0, 1);
long t2 = calenderObj.getTimeInMillis();
long ldap = (t1 - t2) * 10000;

希望这会有所帮助。