How to make Date locale-independent?

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

    Here's a snippet I used to calculate the GMT offset from the Calendar instance and format it. I appreciate all the help I've gotten from this site, its nice to contribute. I hope this helps someone somewhere. Enjoy.

    Calendar calInst = Calendar.getInstance();
    
    //calculate the offset to keep calendar instance GMT
    int gmtOffsetMilli = calInst.get(Calendar.ZONE_OFFSET);
    long gmtOffsetHr = TimeUnit.HOURS.convert(gmtOffsetMilli, TimeUnit.MILLISECONDS);
    
    calInst = Calendar.getInstance(TimeZone.getTimeZone("GMT " + gmtOffsetHr));
    

提交回复
热议问题