在Java中将带有'+'的时间戳解析为Date或Instant [英] Parsing timestamp with '+' into Date or Instant in Java
本文介绍了在Java中将带有'+'的时间戳解析为Date或Instant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有格式为2020-05-12 12:00:00+00
的时间戳.如何解析为Java.util.Date和Java.time.Instant.
看似基本的问题,但我怀疑这是我尚未在
I have timestamp of the format 2020-05-12 12:00:00+00
. How can I parse into Java.util.Date and Java.time.Instant.
Seemingly basic question, but I suspect it is the source of the problem that I am yet to solve in thread
推荐答案
Java 8引入了DateTimeFormatter
.这是 DateTimeFormatter文档.
Java 8 introduced DateTimeFormatter
. Here is the link to the DateTimeFormatter Documentation.
例如:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ssx");
String tsFromDb = "2020-05-12 12:00:00+00";
Instant inst = formatter.parse(tsFromDb, Instant::from);
System.out.println(inst);
输出:
2020-05-12T12:00:00Z
2020-05-12T12:00:00Z
如果您需要java.util.Date
:
Date oldfashionedDate = Date.from(inst);
System.out.println(oldfashionedDate);
美国/蒂华纳时区的输出:
Output in America/Tijuana time zone:
2020年5月12日星期二05:00:00 PDT
Tue May 12 05:00:00 PDT 2020
这篇关于在Java中将带有'+'的时间戳解析为Date或Instant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文