Date
object is a common requirement for Java developers working with Active Directory data. This blog post will guide you through the process, covering core concepts, typical usage scenarios, common pitfalls, and best practices.As mentioned earlier, an Active Directory timestamp is a 18 - digit number representing the number of 100 - nanosecond intervals since January 1, 1601 (UTC). For example, if you query an Active Directory object and get a timestamp value like 132798720000000000
, it needs to be converted to a Java Date
object to make it easier to understand and work with.
In Java, the java.util.Date
class represents a specific instant in time, with millisecond precision. To convert an Active Directory timestamp to a Java Date
, we need to account for the difference in the epoch (January 1, 1601 for Active Directory vs. January 1, 1970 for Java) and the difference in precision (100 - nanoseconds for Active Directory vs. milliseconds for Java).
Date
objects allows for better visualization and analysis. For example, you can group account creation events by month or year.import java.util.Date;
public class ActiveDirectoryTimestampConverter {
// The number of milliseconds between January 1, 1601 and January 1, 1970
private static final long AD_EPOCH_DIFFERENCE = 11644473600000L;
/**
* Converts an 18 - digit Active Directory timestamp to a Java Date object.
* @param adTimestamp The 18 - digit Active Directory timestamp.
* @return A Java Date object representing the same time.
*/
public static Date convertToJavaDate(long adTimestamp) {
// Convert 100 - nanoseconds to milliseconds
long milliseconds = adTimestamp / 10000;
// Adjust for the difference in epochs
long adjustedMilliseconds = milliseconds - AD_EPOCH_DIFFERENCE;
return new Date(adjustedMilliseconds);
}
public static void main(String[] args) {
// Example 18 - digit Active Directory timestamp
long adTimestamp = 132798720000000000L;
Date javaDate = convertToJavaDate(adTimestamp);
System.out.println("Active Directory Timestamp: " + adTimestamp);
System.out.println("Java Date: " + javaDate);
}
}
In this code:
AD_EPOCH_DIFFERENCE
which represents the number of milliseconds between January 1, 1601 (the Active Directory epoch) and January 1, 1970 (the Java epoch).convertToJavaDate
method takes an 18 - digit Active Directory timestamp as input. It first converts the 100 - nanosecond intervals to milliseconds by dividing by 10000. Then, it subtracts the epoch difference to get the number of milliseconds since January 1, 1970. Finally, it creates a new Date
object using the adjusted milliseconds.main
method, we provide an example Active Directory timestamp and call the convertToJavaDate
method to convert it to a Java Date
object and print the results.AD_EPOCH_DIFFERENCE
from the converted milliseconds.Date
objects have millisecond precision. Make sure to convert the 100 - nanosecond intervals to milliseconds by dividing by 10000.int
data type instead of a long
can cause integer overflow. Always use long
to store Active Directory timestamps.Converting 18 - digit Active Directory timestamps to Java Date
objects is a straightforward process once you understand the core concepts of the Active Directory timestamp format and the Java Date
class. By following the code example and best practices outlined in this blog post, you can effectively handle this conversion in real - world scenarios and avoid common pitfalls.
java.time
package instead of java.util.Date
?Yes, you can. The java.time
package introduced in Java 8 provides more modern and flexible date and time APIs. You can convert the adjusted milliseconds to a java.time.Instant
object and then to other java.time
classes as needed.
You need to first convert the string to a long
data type using Long.parseLong()
. Make sure to handle any NumberFormatException
that may occur during the conversion.
There are no widely - used third - party libraries specifically for this conversion. However, libraries like Apache Commons Lang can provide additional utility methods for handling dates and numbers.
java.util.Date
class:
https://docs.oracle.com/javase/8/docs/api/java/util/Date.html