How to make Date locale-independent?

后端 未结 5 1998
我在风中等你
我在风中等你 2021-01-06 10:13

I have a db, that stores dates in OleDateTime format, in GMT timezone. I\'ve implemented a class, extending Date in java to represent that in class

5条回答
  •  盖世英雄少女心
    2021-01-06 10:43

    Well, it's better to use the Calendar object like suggested in other answers. However, if you really want to set global timezone, you can use TimeZone.setDefault(TimeZone.getTimeZone("UTC")); early in your application code. There is also user.timezone Java system property.

    Also (just fun to know), it appears that the only country actually living by GMT/UTC time (without daylight saving changes) is Liberia.

    In fact, Date objects per se are always locale- and timezone-independent. Its getTime() method will always return the number of milliseconds passed since January 1, 1970 00:00:00 (not counting leap seconds) in UTC. But if you want to get something else than milliseconds, you have to use Calendar, which is timezone-dependent. But it is the right way to go. You don't use that deprecated methods in Date class, do you?

提交回复
热议问题