Steinar Seim
Steinar Seim

Reputation: 1

Java's getDate returns wrong date

I'm retrieving some dates from my Parse database. The problem is that one of the retrieved dates from the database returns wrong date. In this case when I'm retrieving the date 01/10/2016 it returns 02/10/2016 instead! The other dates, however, returns the correct date (which in this case is the two other dates in the log (07/02/2016 and 21/02/2016).

What for a strange reason is responsible for returning wrong date only at this one date, while it is returning the correct date on every other?!

(list is a list for the parseObjects on my database, containing the dates)

Date[] surveyDate = new Date[list.size()];
for(int i = 0; i<list.size(); i++){
    surveyDate[i] = list.get(i).getDate("day");
    System.out.println("surveyDate: " +surveyDate[i].getDate() + ", " + (surveyDate[i].getMonth()+1) + ", " + (surveyDate[i].getYear()+1900));
}

Log

I/System.out: surveyDate: date: 7, month: 2, 2016
I/System.out: surveyDate: date: 21, month: 2, 2016
I/System.out: surveyDate: date: 2, month: 10, 2016

Here I've got an image of my dates on the database, and you can see that only the last one (1 October) is returned with wrong value.

By the way, don't worry about the other dates showed in the image, they are ordered and sorted out in list.

Dates on database

enter image description here

Upvotes: 0

Views: 522

Answers (2)

jbernach
jbernach

Reputation: 56

Check the time zone. The date 1/10/2016 in the DB is close to midnight.

Perhaps you are retrieving those dates in GMT and aplying the JVM timezone.

It would help if you print the time also on console.

Upvotes: 2

fikkatra
fikkatra

Reputation: 5792

It is probably related to time zones. Check the time zones of your database server and the server that is retrieving the record, they might be different.

A good practice is to always use datetimeoffset to avoid any confusion.

Upvotes: 0

Related Questions