Handling time zone in web application

后端 未结 6 909
梦毁少年i
梦毁少年i 2020-12-24 03:45

In our web application we need to show and enter date time information for different countries in different time zone. Right now, we are maintaining separate web server and

6条回答
  •  无人及你
    2020-12-24 04:20

    I would simply transform the existing dates stored in the DB into Long, and persist (ETL process) this Long value, along with the known (or deducted) pattern, Locale and TimeZone (default meta). And persist any new Date as long as with the default meta.

    ETL example

    Let say 2015-11-29 10:07:49.500 UTC is stored in the DB:

    // Known or deducted format of the persisted date
    String   pattern = "yyyy-MM-dd HH:mm:ss.SSS"; 
    Locale   locale  = Locale.ENGLISH;
    TimeZone zone    = "UTC";
    
    // Date to ms
    SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
    sdf.setTimeZone(TimeZone.getTimeZone(zone));
    Date date = sdf.parse(pattern);
    
    // ETL: Can now be persisted in Long, along with default META (pattern, Locale, TZ)
    Long dateL = date.getTime(); // for e.g. 1448827660720
    ...
    

    The persisted Long value can also be transformed in any other format, if needed

    pattern                  | locale   | tz   |  result
    ============================================
    yyyy/MM/dd               | null     | null |  2015/11/29          
    dd-M-yyyy hh:mm:ss       | null     | null |  29-11-2015 10:07:40
    dd MMMM yyyy zzzz        | ENGLISH  | null |  29 November 2015 Central European Time
    yyyy-MM-dd HH:mm:ss.SSS  | null     | UTC  |  2015-11-29 10:07:49 UTC
    

提交回复
热议问题