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

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

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

问题

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

代码:

List<LocalDateTime> times = Arrays.asList(
                                 LocalDateTime.now(),
                                 LocalDateTime.parse("2020-09-13T20:53", DateTimeFormatter.ISO_LOCAL_DATE_TIME),
                                 LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
                             );

for (LocalDateTime time: times)
    System.out.println("Time: " + time);

控制台输出:

Time: 2020-09-13T18:42:25.775
Time: 2020-09-13T20:53
Time: 2020-09-13T18:42:25.779

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

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

2020-09-10T09:14:00.001
2020-09-10T09:14:30.001
2020-09-10T09:15:00.001
2020-09-10T09:15:30.001
2020-09-10T09:16:00.001
2020-09-10T09:16:30
2020-09-10T09:17
2020-09-10T09:17:30.001
2020-09-10T09:18:00.001
2020-09-10T09:18:30
2020-09-10T09:19:00.001
2020-09-10T09:19:30.001
2020-09-10T09:20:00.001
2020-09-10T09:20:30
2020-09-10T09:21:00.001
2020-09-10T09:21:30
2020-09-10T09:22:00.001
2020-09-10T09:22:30.001
2020-09-10T09:23
2020-09-10T09:23:30.001
2020-09-10T09:24
2020-09-10T09:24:30
2020-09-10T09:25
2020-09-10T09:25:30
2020-09-10T09:26:00.001
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:

List &lt;LocalDateTime&gt; times = Arrays.asList(
                                 LocalDateTime.now(),
                                 LocalDateTime.parse(&quot;2020-09-13T20:53&quot;, DateTimeFormatter.ISO_LOCAL_DATE_TIME),
                                 LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
                             );

for (LocalDateTime time: times)
    System.out.println(&quot;Time: &quot; + time);

Console:

Time: 2020-09-13T18:42:25.775
Time: 2020-09-13T20:53
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.

2020-09-10T09:14:00.001
2020-09-10T09:14:30.001
2020-09-10T09:15:00.001
2020-09-10T09:15:30.001
2020-09-10T09:16:00.001
2020-09-10T09:16:30
2020-09-10T09:17
2020-09-10T09:17:30.001
2020-09-10T09:18:00.001
2020-09-10T09:18:30
2020-09-10T09:19:00.001
2020-09-10T09:19:30.001
2020-09-10T09:20:00.001
2020-09-10T09:20:30
2020-09-10T09:21:00.001
2020-09-10T09:21:30
2020-09-10T09:22:00.001
2020-09-10T09:22:30.001
2020-09-10T09:23
2020-09-10T09:23:30.001
2020-09-10T09:24
2020-09-10T09:24:30
2020-09-10T09:25
2020-09-10T09:25:30
2020-09-10T09:26:00.001
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() 方法,该方法会截断零毫秒甚至秒数。

例如,可以像这样操作:

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

这将输出

Time: 2020-09-15T15:09:08.011
Time: 2020-09-13T20:53:00.000
Time: 2020-09-15T15:09:08.027

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

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

输出

Time: 2020-09-15T15:12:55.592
Time: 2020-09-13T20:53:00
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:

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

This outputs

Time: 2020-09-15T15:09:08.011
Time: 2020-09-13T20:53:00.000
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

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

outputs

Time: 2020-09-15T15:12:55.592
Time: 2020-09-13T20:53:00
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:

确定