简体   繁体   English

我如何获得自纪元以来经过的微秒长(时间),然后在 Java 8 中从同一时间获得月份和年份

[英]How do I get long(time) in microseconds passed since epoch and then get month and year from the same in Java 8

I found this code:我找到了这段代码:

long timeStampSeconds = ChronoUnit.MICROS.between(Instant.EPOCH, Instant.now());

but when I converted this back to fetch month and year using the following code, it gives the wrong output.但是当我使用以下代码将其转换回获取月份和年份时,它给出了错误的 output。

try {
    epochLong = Long.parseLong(epoch);
} catch (RuntimeException e) {
    //Log exception
    return null;
}

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(epochLong);
String month = String.valueOf(cal.get(Calendar.MONTH));
String year = String.valueOf(cal.get(Calendar.YEAR));
//print month and year

Any pointers as to how I can generate this in microseconds and get the month and year back right?关于如何在微秒内生成它并获得正确月份和年份的任何指示? Please help.请帮忙。

If you are using java.time from Java-8 you can use:如果您使用java.time中的 java.time 您可以使用:

long timeStampSeconds = ChronoUnit.MICROS.between(Instant.EPOCH, Instant.now());

to back you can convert this to an instant, then the instant to the ZoneDateTime回到后面,您可以将其转换为瞬间,然后将瞬间转换为ZoneDateTime

Instant inst = Instant.EPOCH.plus(timeStampSeconds, TimeUnit.MICROSECONDS.toChronoUnit());
ZonedDateTime zdt = inst.atZone(ZoneId.of("Europe/Paris"));

Then you can get the month, year, day, hour, ...然后你可以得到月,年,日,小时,...

Month month = zdt.getMonth();
int year = zdt.getYear();

Outputs输出

MICROS:       1574073141066090
ZoneDateTime: 2019-11-18T11:32:21.066090+01:00[Europe/Paris]

It depends on the platform and the Java version.这取决于平台和 Java 版本。

On my Mac example timeStampSeconds values from your code include在我的 Mac 示例中,您的代码中的timeStampSeconds值包括

  • using jdk-1.8.0_121: 1574193249064000 and 1574193338130000;使用 jdk-1.8.0_121:1574193249064000 和 1574193338130000; so always ending in 000所以总是以 000 结尾
  • using jdk-9.0.4: 1574193430428678 and 1574193438362321;使用 jdk-9.0.4:1574193430428678 和 1574193438362321; so usually not ending in 000.所以通常不以 000 结尾。

On Java 8 the now methods of the java.time classes haven't got any finer granularity than millisenods, so the microsecond of millisecond will always be 000. On Java 9 a finer granularity is available if the platform supports it, which may not be the case for all platforms. On Java 8 the now methods of the java.time classes haven't got any finer granularity than millisenods, so the microsecond of millisecond will always be 000. On Java 9 a finer granularity is available if the platform supports it, which may not be适用于所有平台的情况。

So if you're getting 000 always, the solution is to upgrade at least to Java 9 and run your Java program on a computer that supports microsecond precision.因此,如果您总是获得 000,解决方案是至少升级到 Java 9 并在支持微秒精度的计算机上运行 Java 程序。

Don't use Calendar不要使用日历

The Calendar class is poorly designed and long outdated. Calendar class 设计不佳且早已过时。 Also it never supports any unit finer than milliseconds.此外,它从不支持任何小于毫秒的单位。 To convert your microsecond value back to a date and time object, use for example:要将您的微秒值转换回日期和时间 object,例如使用:

    long timeStampSeconds = 1_574_193_438_362_321L;
    Instant convertedBack = Instant.EPOCH.plus(timeStampSeconds, ChronoUnit.MICROS);
    System.out.println(convertedBack);

2019-11-19T19:57:18.362321Z 2019-11-19T19:57:18.362321Z

You will notice that the full microsecond precision is preserved.您会注意到保留了完整的微秒精度。 YCF_L in another answer has already shown how to get the year and month values.另一个答案中的 YCF_L 已经展示了如何获取年份和月份值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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