0

In my Spring Boot project, I have this array of objects (List<Object> values)

["2021-06-13T08:50:04.707Z", 3197691, 1933]

The first element is a String and represents a timestamp with timezone.

I can get the first element like this:

String time = (String) values.get(0);

How can I convert it in date or time format in order to insert in a PostgreSQL table?

Kirby
  • 15,127
  • 10
  • 89
  • 104
guidop21
  • 177
  • 3
  • 15

2 Answers2

3

The given timestamp string conforms to the ISO 8601 standards.

The modern Date-Time API* is based on ISO 8601 and does not require using a DateTimeFormatter object explicitly as long as the Date-Time string conforms to the ISO 8601 standards.

Demo:

import java.time.OffsetDateTime;

public class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2021-06-13T08:50:04.707Z");
        System.out.println(odt);
    }
}

Output:

2021-06-13T08:50:04.707Z

Check this answer to learn how to use OffsetDateTime with JDBC.

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

First thing you must understand the timestamp format, and for your timestamp format is

yyyy-MM-dd'T'hh:mm:ss.SSS'Z'

Don't forget to add 'T' and 'Z'

Then, to convert from string date to Date class you can use SimpleDateFormat class

String dateInString = "2021-06-13T08:50:04.707Z";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
Date date = dateFormat.parse(dateInString);

Then use the data object to insert to your query. Or if you are using JPA, simply just create the entity and put data object to the variable.

rioswarawan
  • 117
  • 1
  • 4
  • 1
    The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. It is recommended to stop using them completely and switch to the [modern Date-Time API](https://www.oracle.com/technical-resources/articles/java/jf14-Date-Time.html). – Arvind Kumar Avinash Jun 14 '21 at 12:02
  • 1
    @ArvindKumarAvinash thank you for the information. Will read more about this. – rioswarawan Jun 14 '21 at 13:33
  • 1
    rioswarawan - Another thing you should learn is [`'Z'` is not the same as `Z`](https://stackoverflow.com/a/67953075/10819573) – Arvind Kumar Avinash Jun 14 '21 at 17:08
  • 2
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. We have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Also quoting the `Z` in the format pattern string is wrong. The `Z` is an offset (of 0) from UTC and needs to be parsed as an offset, or you will get a wrong point in time. Finally lower case `hh` is wrong too. – Ole V.V. Jun 14 '21 at 17:31
  • 2
    (A) *Never* put the `Z` within single-quotes like this `'Z'`. Doing so tells the parser to ignore what is crucial information: An offset-from-UTC of zero hours-minutes-seconds. (B) And, as others commented, you are using *terrible* classes that were years ago supplanted by the *java.time* classes. See the modern solution in [Answer by Arvind Kumar Avinash](https://stackoverflow.com/a/67968871/642706). – Basil Bourque Jun 14 '21 at 21:14
  • Well, thank you guys. Learn so much about this. – rioswarawan Jun 15 '21 at 01:48