如何在Java 8中使用LocalDateTime.now()获取包括秒的当前LocalDateTime。

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

How to get the current LocalDateTime including seconds with LocalDateTime.now() on Java 8

问题

Java 8中,我使用LocalDateTime.now()来获取本地日期和时间。但有时候这个now()函数返回的时间格式中没有包含秒。我猜想这是因为秒数为零,但我需要秒数。

代码:

  1. List<LocalDateTime> times = Arrays.asList(
  2. LocalDateTime.now(),
  3. LocalDateTime.parse("2020-09-13T20:53", DateTimeFormatter.ISO_LOCAL_DATE_TIME),
  4. LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
  5. );
  6. for (LocalDateTime time: times)
  7. System.out.println("Time: " + time);

控制台输出:

  1. Time: 2020-09-13T18:42:25.775
  2. Time: 2020-09-13T20:53
  3. Time: 2020-09-13T18:42:25.779

Time: 2020-09-13T20:53 这个时间没有包含秒数。

另外,我有一个定时器每30秒运行一次 LocalDatetime.now()。下面是控制台显示的时间戳:

  1. 2020-09-10T09:14:00.001
  2. 2020-09-10T09:14:30.001
  3. 2020-09-10T09:15:00.001
  4. 2020-09-10T09:15:30.001
  5. 2020-09-10T09:16:00.001
  6. 2020-09-10T09:16:30
  7. 2020-09-10T09:17
  8. 2020-09-10T09:17:30.001
  9. 2020-09-10T09:18:00.001
  10. 2020-09-10T09:18:30
  11. 2020-09-10T09:19:00.001
  12. 2020-09-10T09:19:30.001
  13. 2020-09-10T09:20:00.001
  14. 2020-09-10T09:20:30
  15. 2020-09-10T09:21:00.001
  16. 2020-09-10T09:21:30
  17. 2020-09-10T09:22:00.001
  18. 2020-09-10T09:22:30.001
  19. 2020-09-10T09:23
  20. 2020-09-10T09:23:30.001
  21. 2020-09-10T09:24
  22. 2020-09-10T09:24:30
  23. 2020-09-10T09:25
  24. 2020-09-10T09:25:30
  25. 2020-09-10T09:26:00.001
  26. 2020-09-10T09:26:30
英文:

I get LocalDateTime in Java 8 with LocalDateTime.now(). But sometimes this now() function returns the time to me in a format without seconds. I assumed it is because the second are zero, but I need the seconds.

Code:

  1. List &lt;LocalDateTime&gt; times = Arrays.asList(
  2. LocalDateTime.now(),
  3. LocalDateTime.parse(&quot;2020-09-13T20:53&quot;, DateTimeFormatter.ISO_LOCAL_DATE_TIME),
  4. LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
  5. );
  6. for (LocalDateTime time: times)
  7. System.out.println(&quot;Time: &quot; + time);

Console:

  1. Time: 2020-09-13T18:42:25.775
  2. Time: 2020-09-13T20:53
  3. Time: 2020-09-13T18:42:25.779

Time: 2020-09-13T20:53 has not seconds.

PD: I have a scheduler that runs "LocalDatetime.now ()" every 30 seconds. This is what it shows in console.

  1. 2020-09-10T09:14:00.001
  2. 2020-09-10T09:14:30.001
  3. 2020-09-10T09:15:00.001
  4. 2020-09-10T09:15:30.001
  5. 2020-09-10T09:16:00.001
  6. 2020-09-10T09:16:30
  7. 2020-09-10T09:17
  8. 2020-09-10T09:17:30.001
  9. 2020-09-10T09:18:00.001
  10. 2020-09-10T09:18:30
  11. 2020-09-10T09:19:00.001
  12. 2020-09-10T09:19:30.001
  13. 2020-09-10T09:20:00.001
  14. 2020-09-10T09:20:30
  15. 2020-09-10T09:21:00.001
  16. 2020-09-10T09:21:30
  17. 2020-09-10T09:22:00.001
  18. 2020-09-10T09:22:30.001
  19. 2020-09-10T09:23
  20. 2020-09-10T09:23:30.001
  21. 2020-09-10T09:24
  22. 2020-09-10T09:24:30
  23. 2020-09-10T09:25
  24. 2020-09-10T09:25:30
  25. 2020-09-10T09:26:00.001
  26. 2020-09-10T09:26:30

答案1

得分: 7

这是 Java 8 中的 LocalDateTime 的一个特性 - 如果秒数是 "00",那么它将被忽略。

你可以在这里了解更多信息:链接

英文:

That's a feature of LocalDateTime in Java 8 - if the seconds is "00", then it will drop it.

You can learn more about it here

答案2

得分: 6

一种方法是为输出创建一个特定的 DateTimeFormatter,因为将 LocalDateTimeString 连接会使用 toString() 方法,该方法会截断零毫秒甚至秒数。

例如,可以像这样操作:

  1. public static void main(String[] args) {
  2. DateTimeFormatter isoDtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
  3. List<LocalDateTime> times =
  4. Arrays.asList(LocalDateTime.now(),
  5. LocalDateTime.parse("2020-09-13T20:53", isoDtf),
  6. LocalDateTime.parse(LocalDateTime.now().format(isoDtf)));
  7. // 指定输出格式,需要明确指定所需的单位
  8. DateTimeFormatter outputFormatter = DateTimeFormatter
  9. .ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS");
  10. // 并明确输出格式化的 LocalDateTime
  11. for (LocalDateTime time: times)
  12. System.out.println("Time: " + time.format(outputFormatter));
  13. }

这将输出

  1. Time: 2020-09-15T15:09:08.011
  2. Time: 2020-09-13T20:53:00.000
  3. Time: 2020-09-15T15:09:08.027

如果只想要秒数,内置的格式化程序(用于解析的那个)就足够了,因为以下代码

  1. public static void main(String[] args) {
  2. DateTimeFormatter isoDtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
  3. List<LocalDateTime> times =
  4. Arrays.asList(LocalDateTime.now(),
  5. LocalDateTime.parse("2020-09-13T20:53", isoDtf),
  6. LocalDateTime.parse(LocalDateTime.now().format(isoDtf)));
  7. // 并明确输出格式化的 LocalDateTime
  8. for (LocalDateTime time: times)
  9. System.out.println("Time: " + time.format(isoDtf));
  10. }

输出

  1. Time: 2020-09-15T15:12:55.592
  2. Time: 2020-09-13T20:53:00
  3. Time: 2020-09-15T15:12:55.623

并且仅截断零秒分数,但不影响秒数。

英文:

One way would be to create a specific DateTimeFormatter for the output because concatenating the LocalDateTime with a String uses the toString() method, which truncates zero millis or even seconds.

Do it like this, for example:

  1. public static void main(String[] args) {
  2. DateTimeFormatter isoDtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
  3. List &lt;LocalDateTime&gt; times =
  4. Arrays.asList(LocalDateTime.now(),
  5. LocalDateTime.parse(&quot;2020-09-13T20:53&quot;, isoDtf),
  6. LocalDateTime.parse(LocalDateTime.now().format(isoDtf)));
  7. // specify the output format, require the units you need explicitly
  8. DateTimeFormatter outputFormatter = DateTimeFormatter
  9. .ofPattern(&quot;uuuu-MM-dd&#39;T&#39;HH:mm:ss.SSS&quot;);
  10. // and explicitly output the formatter LocalDateTime
  11. for (LocalDateTime time: times)
  12. System.out.println(&quot;Time: &quot;+ time.format(outputFormatter));
  13. }

This outputs

  1. Time: 2020-09-15T15:09:08.011
  2. Time: 2020-09-13T20:53:00.000
  3. Time: 2020-09-15T15:09:08.027

If you just want the seconds, a built-in formatter (the one you are using for parsing) would be sufficient, because the following code

  1. public static void main(String[] args) {
  2. DateTimeFormatter isoDtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
  3. List &lt;LocalDateTime&gt; times =
  4. Arrays.asList(LocalDateTime.now(),
  5. LocalDateTime.parse(&quot;2020-09-13T20:53&quot;, isoDtf),
  6. LocalDateTime.parse(LocalDateTime.now().format(isoDtf)));
  7. // and explicitly output the formatter LocalDateTime
  8. for (LocalDateTime time: times)
  9. System.out.println(&quot;Time: &quot;+ time.format(isoDtf));
  10. }

outputs

  1. Time: 2020-09-15T15:12:55.592
  2. Time: 2020-09-13T20:53:00
  3. Time: 2020-09-15T15:12:55.623

and truncates zero fractions of second only, but doesn't touch seconds.

huangapple
  • 本文由 发表于 2020年9月15日 21:05:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63902489.html
匿名

发表评论

匿名网友

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

确定