Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date parsing from string to long gives wrong result

Tags:

java

timestamp

I got simple code, maybe the problem relies on the given format string or on the timezone. So here is the code:

public static void main(String[] args) {
    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
    try {
        Date added = df.parse("00:00");
        System.out.println(added);
        System.out.println(added.getTime());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The result is:Thu Jan 01 00:00:00 EET 1970 -10800000 --> should be 0 as we give 00:00 hours in and the other time elements remain default.

//Edit

Yes the problem is with timezone to fix this use df.setTimeZone(TimeZone.getTimeZone("UTC")); before parsing.

like image 980
Risto Novik Avatar asked Oct 10 '22 07:10

Risto Novik


1 Answers

The value 10800000 is exactly 3 hours (in milliseconds), which I'm gathering is roughly the offset between EET and UTC (actually, it's only 2 hours according to this, but I guess the extra hour's down to DST or something).

Therefore, the difference is probably due to your timezone.

like image 157
Mac Avatar answered Oct 26 '22 06:10

Mac