如何在java中将字符串转换为日期“yyyy-MM-dd'T'HH:mm:ss.SSS'Z'”

S. *_*ndo -1 java datetime-format

我有一个字符串日期。例如:"2020-02-21 16:36:30.072"我想将其转换为 Date "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"。(即2020-02-21T16:36:30.072+05:30

如何?请你帮助我好吗?

Mad*_*mer 5

您需要首先将其转换String为更具“可塑性”的格式 - 可以表示日期/时间的格式,您可以根据需要生成不同的格式

既然已经是 2020 年了,你应该从java.time.*API 开始。

String input = "2020-02-21 16:36:30.072";
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime ldt = LocalDateTime.parse(input, inputFormatter);
Run Code Online (Sandbox Code Playgroud)

好的,现在,您的部分要求是拥有时区,因此您需要将 转换LocalDateTime为 a ZonedDateTime,从技术上讲,您可以一步完成此操作,但这是一个很好的演示

ZonedDateTime zdt = ldt.atZone(ZoneId.systemDefault());

DateTimeFormatter outputFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
String output = outputFormatter.format(zdt);

System.out.println(input);
System.out.println(output);
Run Code Online (Sandbox Code Playgroud)

这将输出

2020-02-21 16:36:30.072
2020-02-21T16:36:30.072+11:00
Run Code Online (Sandbox Code Playgroud)

我住在澳大利亚,所以我的时区是+10(夏令时+1)。如果您愿意,您可以指定特定的时区,这只是为了演示(我懒得去尝试找出 530+ 时区;))


Bas*_*que 5

tl;博士

这是一个单线。

LocalDateTime
.parse(
    "2020-02-21 16:36:30.072".replace( " " , "T" )
)                                                   // Returns a `LocalDateTime` object. *Not* a moment, *not* a point on the timeline. Just a date and a time-of-day, nothing more. Lacks context of a time zone or offset-from-UTC.
.atZone(                                            // Lending context to our `LocalDateTime` object to determine a moment by assigning a time zone.
    ZoneId.of( "Asia/Kolkata" )                     // Currently using an offset of five and a half hours ahead of UTC.
)                                                   // Returns a `ZonedDateTime` object.
.format(                                            // Generates text representing the value of the `ZonedDateTime` object.
    DateTimeFormatter.ISO_OFFSET_DATE_TIME          // Pre-defined formatter. No need to specify your own formatting pattern. Your desired format complies with the ISO 8601 standard.
)                                                   // Returns a `String`. 
Run Code Online (Sandbox Code Playgroud)

查看此代码在 IdeOne.com 上实时运行

2020-02-21T16:36:30.072+05:30

细节

MadProgrammer答案是正确且详细的。我将采取一些捷径,并专门解决您想要的 +05:30 偏移量。

我们LocalDateTime只需将中间的空格字符替换为大写,就可以将您的输入解析为 a T

String input = "2020-02-21 16:36:30.072".replace( " " , "T" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;
Run Code Online (Sandbox Code Playgroud)

一个LocalDateTime不会表示一下,是不是在时间轴上的一个点。它缺少时区或UTC偏移量的上下文。

您所需的偏移量+05:30目前仅用于两个时区:

  • Asia/Colombo (斯里兰卡)
  • Asia/Kolkata (印度)

选择哪一个是你的。

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
Run Code Online (Sandbox Code Playgroud)

应用于您LocalDateTime的确定时刻,产生一个ZonedDateTime.

ZonedDateTime zdt = ldt.atZone( z ) ;
Run Code Online (Sandbox Code Playgroud)

以标准ISO 8601格式生成一个字符串,明智地扩展以在方括号中附加区域名称。

String output = zdt.toString() ;
Run Code Online (Sandbox Code Playgroud)

如果您真的只想要没有时区的偏移量,请记住,您的数据的读者不会确定您是指斯里兰卡时间还是印度时间。如果您坚持,请使用DateTimeFormatter其他答案中所示的预定义对象。


Java 中的日期时间类型表,现代和传统