如何在Java 8中格式化ZoneDateTime?

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

How to format ZoneDateTime in Java 8?

问题

我想以某种格式来格式化当前日期。

格式:20200807|12:44:55.000

但是使用以下代码只打印到秒。我想要打印到毫秒。

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd|HH:mm:ss.SSS");
String formattedString1 = ZonedDateTime.now().format(formatter1);
System.out.println(formattedString1);
英文:

I want to format current date in some format.

Format : 20200807|12:44:55.000

But by using following code is prints only till seconds. I want to prints till miliseconds.

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd|HH:mm:ss");
String formattedString1 = ZonedDateTime.now().format(formatter1);
System.out.println(formattedString1);

答案1

得分: 3

你可以基本上扩展你正在使用的 String 模式,它目前(显然)只格式化到秒钟单位(HH:mm:ss ⇒ 小时、分钟和秒)。

将它改成 "yyyyMMdd|HH:mm:ss.SSS" 以获得秒的小数部分,

public static void main(String[] args) {
	DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd|HH:mm:ss.SSS");
	String formattedString1 = ZonedDateTime.now().format(formatter1);
	System.out.println(formattedString1);
}

这将输出如下形式的字符串:

20200807|10:57:17.547

如果你需要一天中的毫秒数(无论出于何种原因),你可以使用一个 A 代替那三个 S

查看 Java java.time.DateTimeFormatter 的Java文档 以获取模式字母及其含义的详细信息。

以下是一个小节选:

   ...
   H       小时(0-23)                 数字                0
   m       分钟                         数字                30
   s       秒                           数字                55
   S       秒的小数部分                 分数                978
   A       一天中的毫秒数               数字                1234
   n       秒的纳秒部分                 数字                987654321
   N       一天中的纳秒数               数字                1234000000
   ...
英文:

You can basically extend the pattern String you are using, it currently (and obviously) only formats time units down to seconds (HH:mm:ss ⇒ hours, minutes & seconds).

Make it "yyyyMMdd|HH:mm:ss.SSS" in order to have fraction of second,

public static void main(String[] args) {
	DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd|HH:mm:ss.SSS");
	String formattedString1 = ZonedDateTime.now().format(formatter1);
	System.out.println(formattedString1);
}

this would output a String of the form the following one has:

20200807|10:57:17.547

If you need milliseconds of day (whysoever), you can use a single A instead of those three S.

Have a look at the JavaDocs of java.time.DateTimeFormatter for pattern letters and their meanings.

Here's a small excerpt:

   ...
   H       hour-of-day (0-23)          number            0
   m       minute-of-hour              number            30
   s       second-of-minute            number            55
   S       fraction-of-second          fraction          978
   A       milli-of-day                number            1234
   n       nano-of-second              number            987654321
   N       nano-of-day                 number            1234000000
   ...

答案2

得分: 2

将模式更改以包括毫秒。

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd|HH:mm:ss.SSS");
String formattedString1 = ZonedDateTime.now().format(formatter1);
System.out.println(formattedString1);
英文:

Change the pattern to include milliseconds.

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd|HH:mm:ss.SSS");
String formattedString1 = ZonedDateTime.now().format(formatter1);
System.out.println(formattedString1);

答案3

得分: 1

在你的代码中,你没有考虑毫秒级别的时间格式。所以你需要更新你的模式如下:

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd|HH:mm:ss.SSS");

更多细节可以参考JavaDoc

英文:

In your code you are not considering milliseconds in pattern. So you need to update your pattern as below :

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd|HH:mm:ss.SSS");

For more details you can refer JavaDoc.

huangapple
  • 本文由 发表于 2020年8月7日 16:49:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63298412.html
匿名

发表评论

匿名网友

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

确定