Java Ldap将lastLogonTimestamp转换为DateTime

时间:2015-02-11 12:44:56

标签: java ldap

我在写一个小型Java应用程序。我读出了几个Active Directoy用户属性。其中一个属性是lastLogonTimestamp。

//Specify the attributes to return
          String returnedAtts[]={"accountExpires","lastLogonTimestamp"};
          searchCtls.setReturningAttributes(returnedAtts);

          //Search for objects using the filter
          NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);

          //Loop through the search results
          while (answer.hasMoreElements()) {
               SearchResult sr = (SearchResult)answer.next();

               System.out.println(">>>" + sr.getName());

               //Print out the groups

               Attributes attrs = sr.getAttributes();
               if (attrs != null) {
                    String accountExpires = attrs.get("accountExpires").toString();
                    Attribute lastLogon = attrs.get("lastLogonTimestamp");


               }
          }

但我无法弄清楚,我如何将属性lastLogonTimestamp转换为String或其他东西,以便我可以使用它。 函数.toString不起作用。当我尝试时,我收到错误消息:

Exception in thread "JavaFX Application Thread"       java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)
at   javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java: 1645)
at  com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8216)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1759)
... 43 more
Caused by: java.lang.NullPointerException
at application.ADfunctions.getUserStats(ADfunctions.java:250)
at application.ADfunctions.getADGroupMember(ADfunctions.java:165)
at application.ADfunctions.LDAPAuth(ADfunctions.java:96)
at controller.loginController.loginIsClicked(loginController.java:34)
... 53 more

有人可以帮帮我吗?为什么属性.toString()不起作用。如果它可以工作,我可以解析它,然后将其转换为DateTime。需要帮助。

3 个答案:

答案 0 :(得分:2)

要将Win32文件时字符串转换为Date,请使用:

long fileTime = (Long.parseLong(inputDateString) / 10000L) - + 11644473600000L;
Date inputDate = new Date(fileTime);

要将Date转换为Win32文件时间,请使用:

long fileTime = (inputDate.getTime() + 11644473600000L) * 10000L;
String outputDate = Long.toString(fileTime);

e.g。 131220409910000000将转换为2016/10/27 14-23-11,反之亦然

检查此网址是否有一个不错的在线纪元/文件时间转换器:

http://www.epochconverter.com/ldap

答案 1 :(得分:1)

您可以使用JNA平台将其转换为Java Date。 lastLogonTimeStamp属性包含Windows FILETIME格式化的值,该值定义为自1601年1月1日午夜以来100纳秒间隔的数量。以下是将日期转换为Windows FILETIME并再次返回的示例。

import java.util.Date;
import com.sun.jna.platform.win32.WinBase.FILETIME;

public class WinFileTimeTest {

  public static void main(String[] args) {
    Date d1 = new Date();

    long wft = FILETIME.dateToFileTime(d1);

    int l_int = (int)wft;   
    int u_int = (int)(wft >>> 32);

    Date d2 = FILETIME.filetimeToDate( u_int , l_int);

    System.out.println("Java Date");
    System.out.println(d1);
    System.out.println("--------------");

    System.out.println("Java Date Converted to Windows FILETIME using FILETIME class");
    System.out.println(wft);
    System.out.println("--------------");

    System.out.println("Windows FILETIME converted back to Java Date using FILETIME class");
    System.out.println(d2);
  }
}

答案 2 :(得分:0)

删除尾随z 然后应用dateformat