英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论