我有这个简单的代码:
DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss.SSSSSS Z");
LocalDateTime.now().format(FORMATTER)
Run Code Online (Sandbox Code Playgroud)
然后我会得到以下异常:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
at java.time.LocalDate.get0(LocalDate.java:680)
at java.time.LocalDate.getLong(LocalDate.java:659)
at java.time.LocalDateTime.getLong(LocalDateTime.java:720)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
at java.time.format.DateTimeFormatterBuilder$OffsetIdPrinterParser.format(DateTimeFormatterBuilder.java:3315)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182)
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1745)
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1719)
at java.time.LocalDateTime.format(LocalDateTime.java:1746)
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
目前我们正在使用
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
Run Code Online (Sandbox Code Playgroud)
要为一系列数据格式化外部供应商的时间范围,对于印度,该格式化程序将给出如下时间:
2018-04-26T00:00:00.000 + 0530
但是,我的供应商说他们不能接受这种格式,它必须看起来像
2018-04-26T00:00:00.000 + 05:30
但是,看起来像DateTimeFormatter,无论我选择Z/z/X/x,我都没有得到那种格式的偏移.只是想知道一种将偏移定制为HH的方法:mm?
或者,我需要在第二个时间获得偏移并且我们自己工作吗?
我有以下简单的log4j2配置,它只是将消息记录到控制台stdout和一个文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<RandomAccessFile name="FILE" fileName="app.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</RandomAccessFile>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="FILE" />
<AppenderRef ref="STDOUT" />
</Root>
</Loggers>
</Configuration>
Run Code Online (Sandbox Code Playgroud)
它工作正常,但如果我改为JSON配置,如果不起作用,如果有人有任何线索?
{ "configuration":
{
"appenders": {
"RandomAccessFile": { "name": "FILE", "fileName": "app.log",
"PatternLayout": { "pattern": "%d %p %c{1.} [%t] %m%n" }
},
"Console": { "name": "STDOUT",
"PatternLayout": { "pattern": "%m%n" }
}
},
"loggers": {
"root": { "level": "trace",
"AppenderRef": { "ref": "STDOUT" …Run Code Online (Sandbox Code Playgroud)