我如何转换"2020-03-11 16:27:31"格式为"2020-02-24T11:04:52 + 00:00"Java格式 [英] How I convert "2020-03-11 16:27:31" format to "2020-02-24T11:04:52+00:00" format in java
本文介绍了我如何转换"2020-03-11 16:27:31"格式为"2020-02-24T11:04:52 + 00:00"Java格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我无法理解如何进行转换"2020-03-11 16:27:31"
到"2020-03-11T16:27:31 + 00:00"
.
Hi I am not able to understand how to convert
"2020-03-11 16:27:31"
to "2020-03-11T16:27:31+00:00"
.
我对此一无所知.
日期给定,格式为"2020-03-11 16:27:31"
我希望以这种格式超过日期 "2020-03-11T16:27:31 + 00:00"
I want above date in this format "2020-03-11T16:27:31+00:00"
推荐答案
以下是您可以执行所需操作的方式(如果使用的是Java8):
Here the way you can do what you need (if you're using java8):
String date = "2020-03-11 16:27:31";
String pattern = "yyyy-MM-dd HH:mm:ss";
// formatter with spaces before and after 'T'
DateTimeFormatter f0 = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.appendLiteral('T')
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
.toFormatter();
// formatter without spaces before and after 'T'
DateTimeFormatter f1 = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
.toFormatter();
OffsetDateTime offsetDateTime = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(pattern))
.atOffset(ZoneOffset.ofHoursMinutes(0, 0));
String result = offsetDateTime.format(f1);
但是我建议您阅读@deHaar推荐的java.timi手册.
But I suggest you to read java.timi manual as @deHaar recomended.
这篇关于我如何转换"2020-03-11 16:27:31"格式为"2020-02-24T11:04:52 + 00:00"Java格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文