Java 11 时间 – 是否为今天的时间戳(long)。

huangapple go评论117阅读模式
英文:

Java 11 time - is (long)timestamp today

问题

我在一个Mongo文档字段中有一个cron作业字符串。我通过以下方式获得下一个有效的(长时间)时间:

  1. CronExpression exp = new CronExpression(billing.getReminder());
  2. long nextReminder = exp.getNextValidTimeAfter(new Date()).getTime();

我的想法是检查这个“nextReminder”是否是今天,如果是,就创建一些任务。在Java 11中,最好的方法是什么?

英文:

I have a cronjob string saved in a mongo document field. I get the next valid (long)time by

  1. CronExpression exp = new CronExpression(billing.getReminder());
  2. long nextReminder = exp.getNextValidTimeAfter(new Date()).getTime();

My idea is to check if this "nextReminder" isToday() then create some task.
What is the best way to check it with java 11?

答案1

得分: 2

你可以使用 java.time 进行比较...

有一个 Instant 表示一个时间点,就像 epoch 毫秒时间戳一样(表示为你的 long nextReminder),以及 OffsetDateTime.now() 表示实际的当前时刻 now,以及 LocalDate 表示仅包含日期部分。

你可以使用类似下面的方法来判断 nextReminder 是否是今天

  1. /**
  2. * <p>
  3. * 检查给定时间戳(以 epoch 毫秒表示)的日期(或日期部分)
  4. * 是否与&lt;em&gt;今天&lt;/em&gt;相同(即执行该方法的当天)。&lt;br&gt;
  5. * <strong>需要偏移量以便进行比较&lt;/strong&gt;
  6. * </p>
  7. *
  8. * @param epochMillis 要检查的以 epoch 毫秒表示的时间戳
  9. * @param zoneOffset 要用作比较基准的偏移量
  10. * @return &lt;code&gt;true&lt;/code&gt; 如果参数的日期与今天的日期相等,
  11. * 否则为 &lt;code&gt;false&lt;/code&gt;
  12. */
  13. public static boolean isToday(long epochMillis, ZoneOffset zoneOffset) {
  14. // 根据给定的偏移量从参数中提取日期部分
  15. LocalDate datePassed = Instant.ofEpochMilli(epochMillis)
  16. .atOffset(zoneOffset)
  17. .toLocalDate();
  18. // 然后根据给定的偏移量从 &quot;now&quot; 中提取日期部分
  19. LocalDate today = Instant.now()
  20. .atOffset(zoneOffset)
  21. .toLocalDate();
  22. // 然后返回相等性检查的结果
  23. return datePassed.equals(today);
  24. }

然后只需像这样调用它:

  1. boolean isNextReminderToday = isToday(nextReminder, ZoneOffset.systemDefault());

这将使用系统的时区偏移。或许,ZoneOffset.UTC 也是一个明智的选择。

英文:

You could use java.time for a comparison...

There is an Instant representing a moment in time like a timestamp in epoch milliseconds does (&rArr; your long nextReminder) as well as OffsetDateTime.now() for the actual moment now and LocalDate as the part describing the date-part only.

You could find out if the nextReminder is today by using a method like this:

  1. /**
  2. * &lt;p&gt;
  3. * Checks if the day (or date) of a given timestamp (in epoch milliseconds)
  4. * is the same as &lt;em&gt;today&lt;/em&gt; (the day this method is executed).&lt;br&gt;
  5. * &lt;strong&gt;Requires an offset in order to have a common base for comparison&lt;/strong&gt;
  6. * &lt;/p&gt;
  7. *
  8. * @param epochMillis the timestamp in epoch milliseconds to be checked
  9. * @param zoneOffset the offset to be used as base of the comparison
  10. * @return &lt;code&gt;true&lt;/code&gt; if the dates of the parameter and today are equal,
  11. * otherwise &lt;code&gt;false&lt;/code&gt;
  12. */
  13. public static boolean isToday(long epochMillis, ZoneOffset zoneOffset) {
  14. // extract the date part from the parameter with respect to the given offset
  15. LocalDate datePassed = Instant.ofEpochMilli(epochMillis)
  16. .atOffset(zoneOffset)
  17. .toLocalDate();
  18. // then extract the date part of &quot;now&quot; with respect to the given offset
  19. LocalDate today = Instant.now()
  20. .atOffset(zoneOffset)
  21. .toLocalDate();
  22. // then return the result of an equality check
  23. return datePassed.equals(today);
  24. }

and then just call it like

  1. boolean isNextReminderToday = isToday(nextReminder, ZoneOffset.systemDefault());

which would use the time offset of the system. Maybe, ZoneOffset.UTC could be a smart choice, too.

答案2

得分: 2

The answer by deHaar is correct. However, I felt to write this one because in this case, using the Zone ID (instead of Zone Offset) makes the code a bit simpler and also easier to understand.

  1. import java.time.Instant;
  2. import java.time.LocalDate;
  3. import java.time.ZoneId;
  4. public class Main {
  5. public static void main(String[] args) {
  6. // A test data
  7. long nextReminder = 1597754387710L;
  8. // Your time-zone e.g. Europe/London
  9. ZoneId zoneId = ZoneId.of("Europe/London");
  10. // Next reminder date
  11. Instant instant = Instant.ofEpochMilli(nextReminder);
  12. LocalDate nextReminderDate = instant.atZone(zoneId).toLocalDate();
  13. // Today at the time-zone of Europe/London
  14. LocalDate today = LocalDate.now(zoneId);
  15. if (today.equals(nextReminderDate)) {
  16. System.out.println("The next reminder day is today");
  17. }
  18. }
  19. }
  20. **Output:**
  21. The next reminder day is today
英文:

The answer by deHaar is correct. However, I felt to write this one because in this case, using the Zone ID (instead of Zone Offset) makes the code a bit simpler and also easier to understand.

  1. import java.time.Instant;
  2. import java.time.LocalDate;
  3. import java.time.ZoneId;
  4. public class Main {
  5. public static void main(String[] args) {
  6. // A test data
  7. long nextReminder = 1597754387710L;
  8. // Your time-zone e.g. Europe/London
  9. ZoneId zoneId = ZoneId.of(&quot;Europe/London&quot;);
  10. // Next reminder date
  11. Instant instant = Instant.ofEpochMilli(nextReminder);
  12. LocalDate nextReminderDate = instant.atZone(zoneId).toLocalDate();
  13. // Today at the time-zone of Europe/London
  14. LocalDate today = LocalDate.now(zoneId);
  15. if (today.equals(nextReminderDate)) {
  16. System.out.println(&quot;The next reminder day is today&quot;);
  17. }
  18. }
  19. }

Output:

  1. The next reminder day is today

答案3

得分: 1

使用 apache commons 的 DateUtils.isToday(nextReminder)

使用你自己的方法。

  1. private static final long MILLIS_PER_DAY = 86400000;
  2. public static boolean isToday(long timestamp) {
  3. long now = System.currentTimeMillis();
  4. long today = now.getTime() / MILLIS_PER_DAY;
  5. long expectedDay = timestamp / MILLIS_PER_DAY;
  6. return today == expectedDay;
  7. }

注意:在处理日期/时间时考虑使用 UTC。

英文:

Using apache commons DateUtils.isToday(nextReminder)

Using your own method.

  1. private static final long MILLIS_PER_DAY = 86400000;
  2. public static boolean isToday(long timestamp) {
  3. long now = System.currentTimeMillis();
  4. long today = now.getTime() / MILLIS_PER_DAY;
  5. long expectedDay = timestamp / MILLIS_PER_DAY;
  6. return today == expectedDay;
  7. }

Note: Consider using UTC when working with date/time.

huangapple
  • 本文由 发表于 2020年8月18日 20:13:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63468352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定