Uma*_*ury 3 java jodatime spring-boot java-time
我正在尝试更新我现有的 elasticsearch springboot 项目,由于源代码相当旧,它仍然使用 joda 时间。现在我要把Joda时间的所有功能升级为java时间。现在在项目中我们使用 Joda Time 的 Date Time
Joda Time 的代码示例
DateTime target = new DateTime(String targetDate, UTC);
Run Code Online (Sandbox Code Playgroud)
我们当前在代码中使用此函数将字符串转换为日期。使用此函数的字符串
2022-10-01T00:00:00.000
被转换为
2022-10-01T00:00:00.000Z
我正在尝试在 java 时间中复制相同的内容。
我尝试解析targetDateusing OffsetDateTimeandZonedDateTime但都给了我错误。
无法在索引 24 处解析文本“2022-10-01T00:00:00.0000”
经过一番尝试后,我能够通过使用继续前进LocalDateTime
LocalDateTime target = LocalDateTime.parse(String targetDate);
Run Code Online (Sandbox Code Playgroud)
它能够解析字符串,但格式不正确,我得到的格式是
2022-10-01T00:00Z
我还尝试将格式化程序与 LocalDateTime 一起使用
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'");
LocalDateTime target= LocalDateTime.parse(targetDate,formatter);
Run Code Online (Sandbox Code Playgroud)
但这仍然给了我错误
无法在索引 24 处解析文本“2022-10-01T00:00:00.0000”
现在我对此有点困惑。
任何帮助表示赞赏。如果我提出问题或格式的方式在任何时候都是错误的,请纠正我。
问候。
编辑:抱歉造成混乱,但正如所指出的,我应该提到我希望返回的值作为java.time 日期时间对象而不是字符串,以便我可以进一步对其执行一些逻辑。非常遗憾。
感谢致敬
可以String "2022-10-01T00:00:00.000"解析为 a,LocalDateTime因为它仅包含年、年月、月日、小时、分钟、分钟秒和秒的小数部分。
您所需的输出String "2022-10-01T00:00:00.000Z"表示相同的值加上偏移量,即Z祖鲁时间,基本上意味着 UTC。
如果要使用向输入添加偏移量,可以将其解析为 a ,然后附加所需的偏移量,这会产生. 您可以使用 打印所需的格式,可以使用预构建的格式,也可以自己定义格式。Stringjava.timeLocalDateTimeOffsetDateTimeDateTimeFormatter
这是一个小例子:
public static void main(String[] args) {
// input value
String dateTime = "2022-10-01T00:00:00.000";
// parse it and append an offset
OffsetDateTime odt = LocalDateTime.parse(dateTime).atOffset(ZoneOffset.UTC);
// define a formatter that formats as desired
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX");
// and print the OffsetDateTime using that formatter
System.out.println(odt.format(dtf));
}
Run Code Online (Sandbox Code Playgroud)
输出:
2022-10-01T00:00:00.000Z
Run Code Online (Sandbox Code Playgroud)
我的示例中只有一个实例OffsetDateTime具有以下值:
该实例OffsetDateTime可用于计算(例如加/减天、月或其他单位),并且可以将其格式化为String. 它还有一个toString()我们无法控制的方法,但如果您没有显式格式化它,则会使用它。
以下几行(第一行是上面示例的最后一行)显示了一些不同的用法:
// print formatted by the DateTimeFormatter from the above example
System.out.println(odt.format(dtf));
// print the object directly, implicitly using its toString()
System.out.println(odt);
// print formatted by a prebuilt DateTimeFormatter (several are available)
System.out.println(odt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
// print with a formatter that uses locale dependant expressions like month names
System.out.println(odt.format(
DateTimeFormatter.ofPattern("EEEE, MMM dd HH:mm:ss xxx",
Locale.ENGLISH)));
Run Code Online (Sandbox Code Playgroud)
最后一个还使用不同的 UTC 表示形式:而不是Z显示以小时和分钟为单位的偏移量。
输出:
// print formatted by the DateTimeFormatter from the above example
System.out.println(odt.format(dtf));
// print the object directly, implicitly using its toString()
System.out.println(odt);
// print formatted by a prebuilt DateTimeFormatter (several are available)
System.out.println(odt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
// print with a formatter that uses locale dependant expressions like month names
System.out.println(odt.format(
DateTimeFormatter.ofPattern("EEEE, MMM dd HH:mm:ss xxx",
Locale.ENGLISH)));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
318 次 |
| 最近记录: |