java Date object wrongly calculated -
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 d2 , d3 produce same date although given different ms value?
edit:
i wanted know why happening, , found out daylight saving time blame on one
although date
object has nothing timezone
(in java, date represents point in time) when call tostring()
, uses platform/system's default timezones , shows date/time accordingly.
if need print dates same time zone can set default
timezone program (during app startup) prior printing 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 dates utc
timezone.
Comments
Post a Comment