简体   繁体   中英

How to convert local time to LDAP timestamp in Java

I want to get two timestamps in LDAP format one has to be the beginning of the day and other has to be end of the day. It can be done by using Java 8 (using Joda-Time) but is there a way out using lower version like java 7.

This is the closest solution I found online, but I do not know how to get the timestamp of start of the day and end of the day. Additionally I need to check for 15 days back (start of the day end of the day from current system time)

To convert a Win32 filetime string to Date, use:

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

To convert a Date to Win32 filetime, use:

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

eg 131220409910000000 will be converted to 2016/10/27 14-23-11 and vice versa

Check this url for a nice online epoch/filetime converter:

http://www.epochconverter.com/ldap

java.time and ThreeTen Backport

    Instant ldapEpoch = Instant.parse("1601-01-01T00:00:00Z");
    ZoneId zone = ZoneId.of("America/Port-au-Prince");

    LocalDate date = LocalDate.now(zone);
    Instant startOfDay = date.atStartOfDay(zone).toInstant();
    Duration sinceLdapEpoch = Duration.between(ldapEpoch, startOfDay);
    assert sinceLdapEpoch.getNano() == 0 : sinceLdapEpoch;
    long ldapTimestamp = sinceLdapEpoch.getSeconds() * 10_000_000;
    System.out.println(ldapTimestamp);

Output is (tested on jdk1.7.0_67):

132114384000000000

The start of the day differs from time zone to time zone. I have used America/Port-au-Prince as an example, you need to make your own pick. Use ZoneOffset.UTC if that is what you require.

For the end of the day use date.plusDays(1) instead of date in the same calculation. That will give you the first moment of the following day. Can you have the last moment of the current day instead? No, and you shouldn't want to either. There is no last moment of a day. A day is a half-open time interval from the first moment of the day inclusive to the first moment of the next day exclusive. The best and the correct way to do in programming is to handle it as such (you may of course cheat and subtract 1 from the result you get to obtain the last possible LDAP timestamp of the day , not recommended).

I am using java.time, the modern Java date and time API. We don't want to use Date or Calendar or other of the poorly designed and long outdated date and time classes in Java. java.time is so much nicer to work with.

Question: is there a way out using lower version like java 7?

Yes, java.time works nicely on Java 7. It just requires at least Java 6 .

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It's called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

You could try the following:

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.clear();
c.set(2016, 4, 22); //your date
long time1= c.getTimeInMillis();
c.set(1601, 0, 1);
long time2= c.getTimeInMillis();
long ldap = (time1- time2) * 10000;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM