Anil Sharma
Anil Sharma

Reputation: 558

java.util.date and long unexpected behavior

I was writing a program and found a behavior which was tough for me to understand. so pasting the code

public class test { 

    public static void main(String args[]) throws ParseException {
        DateFormat formatter = new SimpleDateFormat("hh:mm");
        Date systemTime2 = formatter.parse("10:36");
        Date systemTime1 = formatter.parse("12:00");
        System.out.println(getMinutesDifference(systemTime2, systemTime1));
        ;
    }
    private static Long getMinutesDifference(Date upperTime, Date lowerTime) {
        System.out.println(upperTime.getTime());
        System.out.println(lowerTime.getTime());
        return (upperTime.getTime() - lowerTime.getTime()) / (1000 * 60);

    }
}

now the output that i am getting is

66960000    
28800000  
636

i was expecting a negative output. i am not able to understand this why is this happening.

Upvotes: 0

Views: 92

Answers (1)

Rob
Rob

Reputation: 6497

In your format, the "hh" hour in AM/PM (1-12). It is not clear to me what exactly you are trying to accomplish, but perhaps using "HH" (hour in day - 0-23) might give you the result you are looking for.

Upvotes: 3

Related Questions