online java compilers gives different results than eclipse and intelliJ

166 views Asked by At

The code simply extract the time portion of two timestamps and sub them from each other to get the duration

import java.util.Calendar;
public class Test {

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(1659304800000L);
        getTimePortionFromCalendar(cal);
        Long startMillis = cal.getTimeInMillis();
        cal.setTimeInMillis(1660089600000L);
        getTimePortionFromCalendar(cal);
        Long endMillis = cal.getTimeInMillis();
        long duration =endMillis - startMillis;

        System.out.println(duration);
    }

    private static void getTimePortionFromCalendar(Calendar start) {
        start.set(1970,Calendar.JANUARY,1, start.get(Calendar.HOUR_OF_DAY), start.get(Calendar.MINUTE), start.get(Calendar.SECOND));
    }
}

Using eclipse and intelliJ yielded the expected answer (7200000) while all online compilers such as https://www.tutorialspoint.com/compile_java_online.php

https://www.onlinegdb.com/online_java_compiler

https://www.programiz.com/java-programming/online-compiler/

https://www.jdoodle.com/online-java-compiler/

printed (-79200000) as the result

Any Idea why ?

0

There are 0 answers