如何在Java中将本地时间转换为LDAP时间戳 [英] How to convert local time to LDAP timestamp in Java

查看:200
本文介绍了如何在Java中将本地时间转换为LDAP时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得LDAP格式的两个时间戳,一个必须是一天的开始,另一个必须是一天的结束.可以使用Java 8(使用Joda-Time)来完成,但是使用Java 7这样的较低版本是否有出路?

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.

这是我在网上找到的最接近的解决方案,但我不知道如何获取当天开始和结束的时间戳.另外,我需要检查15天(从当前系统时间开始一天结束)

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)

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

To convert a Win32 filetime string to Date, use:

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

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

To convert a Date to Win32 filetime, use:

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

例如131220409910000000将转换为2016/10/27 14-23-11,反之亦然

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

检查此URL,以获得一个不错的在线纪元/文件时间转换器:

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

http://www.epochconverter.com/ldap

推荐答案

java.time 和 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);

输出(在jdk1.7.0_67上测试):

Output is (tested on jdk1.7.0_67):

132114384000000000

132114384000000000

一天的开始时间因时区而异.我以美国/太子港为例,您需要自己选择.如果需要,请使用 ZoneOffset.UTC .

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.

在一天结束时,在同一计算中使用 date.plusDays(1)而不是 date .那将给您第二天的第一刻.您可以改用当天的最后一刻吗?不,您也不想.没有一天的最后时刻.一天是从一天中的第一天到第二天的第一天的半开放时间间隔.编程中最好和正确的方法是这样处理(当然,您可能会作弊并从获得当天最后可能的LDAP时间戳的结果中减去1,不建议这样做).

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).

我正在使用现代Java日期和时间API java.time.我们不想使用Java中的 Date Calendar 或其他设计欠佳且过时的日期和时间类.java.time更好用.

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.

是的,java.time在Java 7上运行良好.它至少需要 Java 6 .

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

  • 在Java 8和更高版本以及更新的Android设备(API级别26起)中,内置了现代API.
  • 在 Java 6 和 7 中获得 ThreeTen Backport,这是现代类的 backport(ThreeTen for JSR 310;请参阅底部的链接).
  • 在(较旧的)Android上,使用Android版本的ThreeTen Backport.称为ThreeTenABP.并确保使用子包从 org.threeten.bp 导入日期和时间类.
  • 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.
  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

这篇关于如何在Java中将本地时间转换为LDAP时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆