-1

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
mobileDev
  • 1,358
  • 2
  • 14
  • 34
  • 1
    Why did you tag `jodatime`, then explicit ask about the built-in `java.util.Date` and `java.time.Instant`? – Andreas May 17 '20 at 02:25
  • I agree with you that you should solve the root cause in your other question. If there is one. I have taken a look and am not convinced. – Ole V.V. May 17 '20 at 06:53

1 Answers1

1

Java 8 introduced DateTimeFormatter. Here is the link to the DateTimeFormatter Documentation.

For example:

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);

Output:

2020-05-12T12:00:00Z

If you need a java.util.Date:

Date oldfashionedDate = Date.from(inst);
System.out.println(oldfashionedDate);

Output in America/Tijuana time zone:

Tue May 12 05:00:00 PDT 2020

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
rocketman
  • 131
  • 1
  • 9
  • It's a good suggestion. It's hardly enough for an answer. Link-only answers are not considered true answers here. You may want to explain in just a bit of depth how it helps. – Ole V.V. May 17 '20 at 05:58
  • @rocketman: Great answer and it worked. – mobileDev May 17 '20 at 17:22