Skip to content
Advertisement

Java Instant.parse cannot validate date

I have an instant field and trying to validate the values in this field using the following approach:

Instant.parse("2021-09-29 09:35:07.531")

However, it throws the error:

java.time.format.DateTimeParseException: Text ‘2021-09-29 09:35:07.531’ could not be parsed at index 10.

So, how can I test if the given Instant date in String format is a valid Instant date?

I am implementing an Import feature and I need to check the date cells. So, the date is normally kept in Instant format in db and for this reason I need to check if the cell date value is a valid Instant date. So, if there is a more proper way, of course I can follow that approach.

I do not use the value, I just need to check if the date is a valid date.

Advertisement

Answer

Your Date-Time string does not have timezone information and therefore you can not parse it into an Instant without introducing a timezone. I recommend you parse it into LocalDateTime and use the same for DB work if it is supposed to be used independent of timezones.

Demo:

JavaScript

Output:

JavaScript

ONLINE DEMO

How to use LocalDateTime in JDBC?

Given below is a sample code to insert a LocalDateTime into columnfoo (which is of TIMESTAMP type):

JavaScript

Given below is a sample code to retrieve a LocalDateTime from columnfoo:

JavaScript

In case you want to parse the given Date-Time string into Instant:

As described above, you need to introduce a timezone in order to parse it into an Instant.

Demo:

JavaScript

Output in my timezone, Europe/London:

JavaScript

ONLINE DEMO

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


* 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. Note that Android 8.0 Oreo already provides support for java.time.

Advertisement