-3

The following program produces incorrect output:

public class date
{
  public static void main(String[] args)
  {
    Date d1 = new Date(1698526800000L);
    Date d2 = new Date(1698530400000L);
    Date d3 = new Date(1698534000000L);
    Date d4 = new Date(1698537600000L);
    System.out.println(d1);
    System.out.println(d2);
    System.out.println(d3);
    System.out.println(d4);
  }
}

result:

Sun Oct 29 00:00:00 IDT 2023
Sun Oct 29 01:00:00 IDT 2023
Sun Oct 29 01:00:00 IST 2023
Sun Oct 29 02:00:00 IST 2023

Why do d2 and d3 produce the same date although given different ms value?

EDIT:

i only wanted to know WHY this was happening, and i found out Daylight saving time was to blame on this one

tchvu3
  • 23
  • 4
  • 1
    Java != JavaScript, and this has nothing to do with the [tag:object] tag. Please choose only relevant tags. – T.J. Crowder Apr 07 '17 at 15:03
  • 11
    The Timezone is different. So its not the same date – Sudipta Mondal Apr 07 '17 at 15:03
  • 3
    Clock change occurs at the end of October (in countries that do it). Try it a day earlier or later and you won't see this "problem". – Reinstate Monica Cellio Apr 07 '17 at 15:05
  • 1
    @SudiptaMondal: Well, the time zone is the same, but it's observing daylight saving time for the second line and standard time for the third line. Time zone != offset at a particular moment in time. – Jon Skeet Apr 07 '17 at 15:06
  • 6
    Suggestion: always start with the assumption that it's your understanding that is flawed, not the tools you're working with. Your assertions in the title and first sentence are incorrect. – Jon Skeet Apr 07 '17 at 15:07
  • @JonSkeet I agree that Time Zone != offset ofcourse. I just thought that IST was Indian standard time and not Israel standard time somehow, a wrong assumption on my part. – Sudipta Mondal Apr 07 '17 at 15:14
  • Your UTC output will be: Sat Oct 28 21:00:00 UTC 2023, Sat Oct 28 22:00:00 UTC 2023, Sat Oct 28 23:00:00 UTC 2023, Sun Oct 29 00:00:00 UTC 2023 so d2 and d3 are off by 4000 sec which is almost an hour. ignoring the ms part that is correct. – Imran Saeed Apr 07 '17 at 15:17
  • Possible duplicate of [Java Convert Long to Date?](http://stackoverflow.com/questions/7487460/java-convert-long-to-date) – Imran Saeed Apr 07 '17 at 15:24
  • @i.net I don't think the OP is asking how to do anything. They're just asking why they didn't get the results they expected. – Reinstate Monica Cellio Apr 07 '17 at 15:37
  • 1
    I know that feeling of “this must be a bug in the system” all too well. Experience has taught me humility: it very rarely is when I think that way. – Ole V.V. Apr 07 '17 at 15:46
  • 1
    i simply wanted to know WHY that's happening, i didn't meant that the way this class is implemented is flawed, and i found out, it was because of Daylight saving time – tchvu3 Apr 07 '17 at 16:46

1 Answers1

0

Although Date object has nothing to do with TimeZone (in Java, date just represents a point in time) when you call toString(), it uses platform/system's default timezones and shows date/time accordingly.

If you need to print all the dates with same time zone then you can set default timezone for your program (during app startup) prior to printing the dates, e.g.:

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Date d1 = new Date(1698526800000L);
Date d2 = new Date(1698530400000L);
Date d3 = new Date(1698534000000L);
Date d4 = new Date(1698537600000L);
System.out.println(d1);
System.out.println(d2);
System.out.println(d3);
System.out.println(d4);

The above should print all the dates with UTC timezone.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • thanks for the comment, i actually had the same problem with js, do you know how to cope with the same problem in js? – tchvu3 Apr 07 '17 at 15:25