Java 11 时间 - 是今天的(长)时间戳
Java 11 time - is (long)timestamp today
我在 mongo 文档字段中保存了一个 cronjob 字符串。我在
之前获得下一个有效(长)时间
CronExpression exp = new CronExpression(billing.getReminder());
long nextReminder = exp.getNextValidTimeAfter(new Date()).getTime();
我的想法是检查这个“nextReminder”是否是 Today() 然后创建一些任务。
使用 java 11 检查它的最佳方法是什么?
使用 Apache Commons DateUtils.isToday(nextReminder)
使用你自己的方法。
private static final long MILLIS_PER_DAY = 86400000;
public static boolean isToday(long timestamp) {
long now = System.currentTimeMillis();
long today = now.getTime() / MILLIS_PER_DAY;
long expectedDay = timestamp / MILLIS_PER_DAY;
return today == expectedDay;
}
注意:在使用 date/time 时考虑使用 UTC。
您可以使用 java.time
进行比较...
有一个 Instant
代表一个时刻,就像以纪元毫秒为单位的时间戳一样(⇒ 你的 long nextReminder
)以及代表实际时刻的 OffsetDateTime.now()
现在和LocalDate
作为描述date-part的部分。
您可以使用如下方法查明 nextReminder
是否是 今天 :
/**
* <p>
* Checks if the day (or date) of a given timestamp (in epoch milliseconds)
* is the same as <em>today</em> (the day this method is executed).<br>
* <strong>Requires an offset in order to have a common base for comparison</strong>
* </p>
*
* @param epochMillis the timestamp in epoch milliseconds to be checked
* @param zoneOffset the offset to be used as base of the comparison
* @return <code>true</code> if the dates of the parameter and today are equal,
* otherwise <code>false</code>
*/
public static boolean isToday(long epochMillis, ZoneOffset zoneOffset) {
// extract the date part from the parameter with respect to the given offset
LocalDate datePassed = Instant.ofEpochMilli(epochMillis)
.atOffset(zoneOffset)
.toLocalDate();
// then extract the date part of "now" with respect to the given offset
LocalDate today = Instant.now()
.atOffset(zoneOffset)
.toLocalDate();
// then return the result of an equality check
return datePassed.equals(today);
}
然后就像
一样称呼它
boolean isNextReminderToday = isToday(nextReminder, ZoneOffset.systemDefault());
这将使用系统的时间偏移。也许,ZoneOffset.UTC
也是一个明智的选择。
是正确的。然而,我觉得写这个是因为在这种情况下,使用区域 ID(而不是区域偏移)使代码更简单,也更容易理解。
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
// A test data
long nextReminder = 1597754387710L;
// Your time-zone e.g. Europe/London
ZoneId zoneId = ZoneId.of("Europe/London");
// Next reminder date
Instant instant = Instant.ofEpochMilli(nextReminder);
LocalDate nextReminderDate = instant.atZone(zoneId).toLocalDate();
// Today at the time-zone of Europe/London
LocalDate today = LocalDate.now(zoneId);
if (today.equals(nextReminderDate)) {
System.out.println("The next reminder day is today");
}
}
}
输出:
The next reminder day is today
我在 mongo 文档字段中保存了一个 cronjob 字符串。我在
之前获得下一个有效(长)时间CronExpression exp = new CronExpression(billing.getReminder());
long nextReminder = exp.getNextValidTimeAfter(new Date()).getTime();
我的想法是检查这个“nextReminder”是否是 Today() 然后创建一些任务。 使用 java 11 检查它的最佳方法是什么?
使用 Apache Commons DateUtils.isToday(nextReminder)
使用你自己的方法。
private static final long MILLIS_PER_DAY = 86400000;
public static boolean isToday(long timestamp) {
long now = System.currentTimeMillis();
long today = now.getTime() / MILLIS_PER_DAY;
long expectedDay = timestamp / MILLIS_PER_DAY;
return today == expectedDay;
}
注意:在使用 date/time 时考虑使用 UTC。
您可以使用 java.time
进行比较...
有一个 Instant
代表一个时刻,就像以纪元毫秒为单位的时间戳一样(⇒ 你的 long nextReminder
)以及代表实际时刻的 OffsetDateTime.now()
现在和LocalDate
作为描述date-part的部分。
您可以使用如下方法查明 nextReminder
是否是 今天 :
/**
* <p>
* Checks if the day (or date) of a given timestamp (in epoch milliseconds)
* is the same as <em>today</em> (the day this method is executed).<br>
* <strong>Requires an offset in order to have a common base for comparison</strong>
* </p>
*
* @param epochMillis the timestamp in epoch milliseconds to be checked
* @param zoneOffset the offset to be used as base of the comparison
* @return <code>true</code> if the dates of the parameter and today are equal,
* otherwise <code>false</code>
*/
public static boolean isToday(long epochMillis, ZoneOffset zoneOffset) {
// extract the date part from the parameter with respect to the given offset
LocalDate datePassed = Instant.ofEpochMilli(epochMillis)
.atOffset(zoneOffset)
.toLocalDate();
// then extract the date part of "now" with respect to the given offset
LocalDate today = Instant.now()
.atOffset(zoneOffset)
.toLocalDate();
// then return the result of an equality check
return datePassed.equals(today);
}
然后就像
一样称呼它boolean isNextReminderToday = isToday(nextReminder, ZoneOffset.systemDefault());
这将使用系统的时间偏移。也许,ZoneOffset.UTC
也是一个明智的选择。
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
// A test data
long nextReminder = 1597754387710L;
// Your time-zone e.g. Europe/London
ZoneId zoneId = ZoneId.of("Europe/London");
// Next reminder date
Instant instant = Instant.ofEpochMilli(nextReminder);
LocalDate nextReminderDate = instant.atZone(zoneId).toLocalDate();
// Today at the time-zone of Europe/London
LocalDate today = LocalDate.now(zoneId);
if (today.equals(nextReminderDate)) {
System.out.println("The next reminder day is today");
}
}
}
输出:
The next reminder day is today