Rest webservice sends JSON date format in request, and I need to validate if this date is past date or not

46 Views Asked by At

Request:

{"start date":"2025-01-15T00:00:01.000z"}

Java code to validate this date is past date or not.

1

There are 1 best solutions below

0
Basil Bourque On

tl;dr

Instant
.parse( "2025-01-15T00:00:01.000Z" ) 
.isBefore( Instant.now() )

ISO 8601

You have text in standard ISO 8601 format. The Z on the end indicates the text represents a moment as seen with an offset from UTC of zero hours-minutes-seconds.

The Z should be uppercase. I assume your example has a typo with its lowercase z.

Instant

Parse as a java.time.Instant.

Instant startDate = Instant.parse( "2025-01-15T00:00:01.000Z" ) ;

Capture the current moment.

Instant now = Instant.now() ;

Compare.

boolean isPast = startDate.isBefore( now ) ;