TimeZone validation in Java

前端 未结 9 2217
梦如初夏
梦如初夏 2021-02-20 00:08

I have a string, I need to check whether it is a standard time zone identifier or not. I am not sure which method I need to use.

String timeZoneToCheck = \"UTC\"         


        
相关标签:
9条回答
  • 2021-02-20 00:13
    private boolean isValidTimeZone(final String timeZone) {
        final String DEFAULT_GMT_TIMEZONE = "GMT";
        if (timeZone.equals(DEFAULT_GMT_TIMEZONE)) {
            return true;
        } else {
            // if custom time zone is invalid,
            // time zone id returned is always "GMT" by default
            String id = TimeZone.getTimeZone(timeZone).getID();
            if (!id.equals(DEFAULT_GMT_TIMEZONE)) {
                return true;
            }
        }
        return false;
    }
    

    The method returns true for the following:

    assertTrue(this.isValidTimeZone("JST"));
    assertTrue(this.isValidTimeZone("UTC"));
    assertTrue(this.isValidTimeZone("GMT"));
    // GMT+00:00
    assertTrue(this.isValidTimeZone("GMT+0"));
    // GMT-00:00
    assertTrue(this.isValidTimeZone("GMT-0"));
    // GMT+09:00
    assertTrue(this.isValidTimeZone("GMT+9:00"));
    // GMT+10:30
    assertTrue(this.isValidTimeZone("GMT+10:30"));
    // GMT-04:00
    assertTrue(this.isValidTimeZone("GMT-0400"));
    // GMT+08:00
    assertTrue(this.isValidTimeZone("GMT+8"));
    // GMT-13:00
    assertTrue(this.isValidTimeZone("GMT-13"));
    // GMT-13:59
    assertTrue(this.isValidTimeZone("GMT+13:59"));
    // NOTE: valid time zone IDs (see TimeZone.getAvailableIDs())
    // GMT-08:00
    assertTrue(this.isValidTimeZone("America/Los_Angeles"));
    // GMT+09:00
    assertTrue(this.isValidTimeZone("Japan"));
    // GMT+01:00
    assertTrue(this.isValidTimeZone("Europe/Berlin"));
    // GMT+04:00
    assertTrue(this.isValidTimeZone("Europe/Moscow"));
    // GMT+08:00
    assertTrue(this.isValidTimeZone("Asia/Singapore"));
    

    ...And false with the following timezones:

    assertFalse(this.isValidTimeZone("JPY"));
    assertFalse(this.isValidTimeZone("USD"));
    assertFalse(this.isValidTimeZone("UTC+8"));
    assertFalse(this.isValidTimeZone("UTC+09:00"));
    assertFalse(this.isValidTimeZone("+09:00"));
    assertFalse(this.isValidTimeZone("-08:00"));
    assertFalse(this.isValidTimeZone("-1"));
    assertFalse(this.isValidTimeZone("GMT+10:-30"));
    // hours is 0-23 only
    assertFalse(this.isValidTimeZone("GMT+24:00"));
    // minutes 00-59 only
    assertFalse(this.isValidTimeZone("GMT+13:60"));
    
    0 讨论(0)
  • 2021-02-20 00:18

    it's simple just use ZoneId.of("Asia/Kolkata")

    try {
        ZoneId.of("Asia/Kolkataa");
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    if your insert invalid time zone it will throw exception

    **java.time.zone.ZoneRulesException: Unknown time-zone ID: Asia/Kolkataa**
    
    0 讨论(0)
  • 2021-02-20 00:25

    This is a more efficient solution, than looping through all possible IDs. It checks the output of getTimeZone.

    Java Docs (TimeZone#getTimeZone):

    Returns: the specified TimeZone, or the GMT zone if the given ID cannot be understood.

    So if the output is the GMT timezone the input is invalid, except if the input accually was "GMT".

    public static boolean isValidTimeZone(@NonNull String timeZoneID) {
        return (timeZoneID.equals("GMT") || !TimeZone.getTimeZone(timeZoneID).getID().equals("GMT"));
    }
    

    Or if you want to use the valid timezone without calling getTimeZone twice:

    TimeZone timeZone = TimeZone.getTimeZone(timeZoneToCheck);
    if(timeZoneToCheck.equals("GMT") || !timeZone.getID().equals("GMT")) {
        // TODO Valid - use timeZone
    } else {
        // TODO Invalid - handle the invalid input
    }
    
    0 讨论(0)
  • 2021-02-20 00:26

    I would like to propose the next workaround:

    public static final String GMT_ID = "GMT";
    public static TimeZone getTimeZone(String ID) {
        if (null == ID) {
            return null;
        }
    
        TimeZone tz = TimeZone.getTimeZone(ID);
    
        // not nullable value - look at implementation of TimeZone.getTimeZone
        String tzID = tz.getID();
    
        // check if not fallback result 
        return GMT_ID.equals(tzID) && !tzID.equals(ID) ? null : tz;
    }
    

    As result in case of invalid timezone ID or invalid just custom timezone you will receive null. Additionally you can introduce corresponding null value handler (use case dependent) - throw exception & etc.

    0 讨论(0)
  • 2021-02-20 00:28

    If TimeZone.getAvailableIDs() contains ID in question, it's valid:

    public boolean validTimeZone(String id) {
        for (String tzId : TimeZone.getAvailableIDs()) {
                if (tzId.equals(id))
                    return true;
        }
        return false;
    }
    

    Unfortunately TimeZone.getTimeZone() method silently discards invalid IDs and returns GMT instead:

    Returns:

    the specified TimeZone, or the GMT zone if the given ID cannot be understood.

    0 讨论(0)
  • 2021-02-20 00:30

    You can get all supported ID using getAvailableIDs()

    Then loop the supportedID array and compare with your String.

    Example:

    String[] validIDs = TimeZone.getAvailableIDs();
    for (String str : validIDs) {
          if (str != null && str.equals("yourString")) {
            System.out.println("Valid ID");
          }
    }
    
    0 讨论(0)
提交回复
热议问题