How to make Date locale-independent?

后端 未结 5 2001
我在风中等你
我在风中等你 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:50

    As Michael Borgwardt has already said, the Java Date object does not know anything about timezones. It's just a wrapper for a number of milliseconds since 01-01-1970 00:00:00 UTC.

    You start dealing with timezones only when you for example convert the Date object to a String using a DateFormat. You set the timezone on the DateFormat to specify in which timezone you want to see the Date.

    DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z");
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    
    String text = df.format(date);  // text will contain date represented in UTC
    

提交回复
热议问题