TimeZone validation in Java

java.time.ZoneId

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**

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

You can write it in one line

public boolean validTimeZone(String timezone) {
    return Set.of(TimeZone.getAvailableIDs()).contains(timezone);
}

Alternatively, you can make a static field for it

private static final Set<String> TIMEZONES = Set.of(TimeZone.getAvailableIDs())
public boolean validTimeZone(String timezone) {
    return TIMEZONES.contains(timezone);
}