2

I've made experiment

for (String tz: TimeZone.getAvailableIDs()) {
    TimeZone.setDefault(TimeZone.getTimeZone(tz));
    timestamp = Timestamp.valueOf("1970-01-01 00:00:00.000");
    System.out.println(tz + " - " + timestamp.getTime());
}

unexpectedly result was different for each timezone. What is the logic behind this? always thought that timestamp is timezone-independant.

x__dos
  • 1,813
  • 3
  • 27
  • 47
  • 1
    `"1970-01-01 00:00:00.000"` had to be interpreted in the context of _some_ timezone, so it uses the default timezone. Did you think it would just use UTC? – GriffeyDog Oct 20 '16 at 15:12
  • @GriffeyDog yep, the problem is that date string comes from DB and it is treated as UTC date – x__dos Oct 20 '16 at 15:14
  • 1
    `String`s don't have a timezone. To convert from a `String` to a `Timestamp`, a timezone needs to be applied. `valueOf` uses the default timezone to make the conversion from `String` to `Timestamp`. – GriffeyDog Oct 20 '16 at 15:16
  • @GriffeyDog hmm strange that Timestamp doesn't support specifying timezone in string then – x__dos Oct 20 '16 at 15:56
  • 1
    See dup link, and also [this answer](http://stackoverflow.com/a/14070771/634824) for a more detailed explanation. – Matt Johnson-Pint Oct 20 '16 at 20:19

1 Answers1

0

Because Timestamp use UTC TimeZone, if you change Timezone It will be different, i.e. when in London was 1970-01-01 00:00:00.000 (GMT+1) in spanish Canary Islands was 1969-12-31 23:00:00.000 (GMT)

*EDIT: For most purposes, UTC is considered interchangeable with GMT

Raider
  • 394
  • 1
  • 2
  • 26