TimeZone validation in Java

2019-04-22 17:14发布

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";

I would like to check whether it represents a valid time zone or not.

8条回答
我命由我不由天
2楼-- · 2019-04-22 17:47

Since TimeZone#getTimeZone(String id) returns a default if the value is invalid (rather than returning an invalid time zone), if the reinterpreted value does not match the initial value, it wasn't valid.

private static boolean isValidTimeZone(@Nonnull String potentialTimeZone)
{
    // If the input matches the re-interpreted value, then the time zone is valid.
    return TimeZone.getTimeZone(potentialTimeZone).getID().equals(potentialTimeZone);
}
查看更多
Summer. ? 凉城
3楼-- · 2019-04-22 17:48

You can use TimeZone.getAvailableIDs() to get list of supported Id

for (String str : TimeZone.getAvailableIDs()) {
        if(str.equals("UTC")){
        //found
  }
}
查看更多
Fickle 薄情
4楼-- · 2019-04-22 17:54

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(input.equals("GMT") || !timeZone.getID().equals("GMT")) {
    //TODO Valid - use timeZone
} else {
    //TODO Invalid - handle the invalid input
}
查看更多
家丑人穷心不美
5楼-- · 2019-04-22 17:56
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"));
查看更多
叼着烟拽天下
6楼-- · 2019-04-22 18:00

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");
      }
}
查看更多
Root(大扎)
7楼-- · 2019-04-22 18:02

You can write it in one line

public boolean validTimeZone(String timezone) {
  return Arrays.asList(TimeZone.getAvailableIDs()).contains(timezone);
}
查看更多
登录 后发表回答