Java Date is one day off from received value

I receive response in PostMan containing date as 2013-01-30

In my Java JSF app, I use Date to store this value in my response model class like:

@XmlRootElement(name="car")
@XmlAccessorType(XmlAccessType.FIELD)
public class CarResponseModel {
   protected Date receivedDate;
   ...
}

However, when the value it gets is one day off like Tue Jan 29 16:00:00 PST 2013.

So, January 30th became January 29th for some reason.

I am looking to fix this so my receivedDate holds correct date 2013-01-30.

I am not able to use LocalDate due to legacy code and incompatibility with JSF version being used.

Answer

You’re seeing the right instant in time for midnight at the start of 2013-01-30 in UTC: 2013-01-30T00:00:00Z and 2013-01-29T00:16:00-08:00 represent the same instant in time. Unfortunately, Date is showing you that in your local time zone. Even if you can’t use LocalDate in that exact spot, I’d suggest you convert the Date into an Instant, then date a LocalDate from that using the UTC time zone.

Leave a Reply

Your email address will not be published. Required fields are marked *