I have to get the count of seconds since 12:00 am December 31, 1989, to now. The second parser comes from Garmin Fleet Management. Here is my code:
public int getDate(){
Date d1 = new Date(1989, 12, 31, 12, 0, 0);
Date today = new Date();
long diff = today.getTime() - d1.getTime();
return (int)(diff/1000);
}
Seconds from getDate() in Garmin parser shows as 08:35 pm July 28, 2021, instead of now.
Here is explained (by documentation) date time that I need
It is an unsigned 32-bit integer and its value is the number of seconds since 12:00 am December 31, 1989, UTC.
Where I made a mistake?
You should return long
and wrap your difference to Math.abs()
for the positive result expressing the difference.
public static long getDate() {
Date d1 = new Date(1989, 12, 31, 12, 0, 0);
Date today = new Date();
long diff = Math.abs(today.getTime() - d1.getTime());
return (diff/1000);
}
There is no unsigned
in Java.
Morover this constructor for Date
is obsolete, so using of Calendar
is the better way:
public static long getDate() {
Calendar d1 = new GregorianCalendar(1989, 11, 31, 0, 0, 0);
Date today = new Date();
long diff = Math.abs(today.getTime() - d1.getTime().getTime());
return (diff/1000);
}