计算 linux 纪元时间属于 Java 的哪一天
Working out which day a linux epoch time belongs in Java
我正在尝试找出给定的 Linux Epoch TS 属于哪一天。
为了帮助解释更多,我给出了以下示例:
我曾尝试使用 JODATIME 解决此问题,但无法获得可行的解决方案。
// some code here that I need.... <---??
// The time set here below is (Tue 2019-09-10 14:05:00 UTC)
final long timestamp = 1568124300000L
if (timestamp = xxxxx) {
System.out.println("The day it belongs to is: " + dayEpoch);
}
理想情况下,我希望打印的输出是:
The day it belongs to is: 2019-09-10
如果您提供实现此目的的代码示例,我们将不胜感激:)
您可以使用 java.time
来满足您的要求:
第一个选项(首选):OffsetDateTime
final long timestamp = 1568124300000L;
// create an Instant from the timestamp
Instant instant = Instant.ofEpochMilli(timestamp);
// create a datetime object from the Instant
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneId.of("UTC"));
// print it in your desired formatting
System.out.println("The day it belongs to is: "
+ offsetDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
第二个选项(有效,但不太合适):ZonedDateTime
final long timestamp = 1568124300000L;
// create an Instant from the timestamp
Instant instant = Instant.ofEpochMilli(timestamp);
// create a datetime object from the Instant
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
// print it in your desired formatting
System.out.println("The day it belongs to is: "
+ zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
Please note that you can have different ZoneId
s. You have clearly written that it is UTC in your case, so I took that one for the example. There is ZoneId.systemDefault();
to get a local timezone, the output for that is the same (date) on my system.
For using this, you will have to be using Java 8 or the ThreeTen-Backport…
我正在尝试找出给定的 Linux Epoch TS 属于哪一天。
为了帮助解释更多,我给出了以下示例:
我曾尝试使用 JODATIME 解决此问题,但无法获得可行的解决方案。
// some code here that I need.... <---??
// The time set here below is (Tue 2019-09-10 14:05:00 UTC)
final long timestamp = 1568124300000L
if (timestamp = xxxxx) {
System.out.println("The day it belongs to is: " + dayEpoch);
}
理想情况下,我希望打印的输出是:
The day it belongs to is: 2019-09-10
如果您提供实现此目的的代码示例,我们将不胜感激:)
您可以使用 java.time
来满足您的要求:
第一个选项(首选):OffsetDateTime
final long timestamp = 1568124300000L;
// create an Instant from the timestamp
Instant instant = Instant.ofEpochMilli(timestamp);
// create a datetime object from the Instant
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneId.of("UTC"));
// print it in your desired formatting
System.out.println("The day it belongs to is: "
+ offsetDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
第二个选项(有效,但不太合适):ZonedDateTime
final long timestamp = 1568124300000L;
// create an Instant from the timestamp
Instant instant = Instant.ofEpochMilli(timestamp);
// create a datetime object from the Instant
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
// print it in your desired formatting
System.out.println("The day it belongs to is: "
+ zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
Please note that you can have different
ZoneId
s. You have clearly written that it is UTC in your case, so I took that one for the example. There isZoneId.systemDefault();
to get a local timezone, the output for that is the same (date) on my system.
For using this, you will have to be using Java 8 or the ThreeTen-Backport…