如何以YYYYMMDD格式格式化日志文件?

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

How to format logfile in YYYYMMDD format?

问题

需要以以下格式获取日期:YYYYMMDD

已有代码输出如下结果。

[2023-02-13 11:05:03] [SEVERE ] 严重消息

System.setProperty("java.util.logging.SimpleFormatter.format",
"[%1$tF %1$tT] [%4$-7s] %5$s %n");

但我想要像这样 [20230213 11:05:03] [SEVERE ] 严重消息

英文:

I need to get the date in the following format: YYYYMMDD

Have code which outputs below result.

[2023-02-13 11:05:03] [SEVERE ] sever message

System.setProperty("java.util.logging.SimpleFormatter.format",
"[%1$tF %1$tT] [%4$-7s] %5$s %n");

But I wanted to be like this [20230213 11:05:03] [SEVERE ] sever message

答案1

得分: 2

要将日期格式更改为YYYYMMDD,您需要修改"java.util.logging.SimpleFormatter.format"属性的值。

尝试将其更改为以下内容:

System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tY%1$tm%1$td %1$tT] [%4$-7s] %5$s %n");

在这个格式中,%1$tY将给您4位数的年份,%1$tm将给您2位数的月份,%1$td将给您2位数的日期。这样,日期将以所需的格式显示。

英文:

To change the format of the date to YYYYMMDD, you need to modify the value of the "java.util.logging.SimpleFormatter.format" property.

Try changing it to the following:

System.setProperty("java.util.logging.SimpleFormatter.format",  [%1$tY%1$tm%1$td %1$tT] [%4$-7s] %5$s %n");

In this format, %1$tY will give you the 4-digit year, %1$tm will give you the 2-digit month, and %1$td will give you the 2-digit day. This way, the date will be displayed in the desired format.

huangapple
  • 本文由 发表于 2023年2月14日 00:24:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438590.html
匿名

发表评论

匿名网友

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

确定