6

I have the following instruction:

TimeZone zone = TimeZone.getTimeZone("Asia/Toyo");

Obviously, it should return null, but it will return the default timeZone, which is not the desired behaviour for my case. From Java Doc:

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

Is there a way to get corresponding TimeZone and null value if the String does not indicate a valid TimeZone?

I don't find a good solution to get all TimeZones and iterate over them.

assylias
  • 321,522
  • 82
  • 660
  • 783
lucian.marcuta
  • 1,250
  • 1
  • 16
  • 29
  • Instead of iterating over all available tzids you might consider to check the result only if it is GMT. If so then check if your input is "GMT" or "UTC" or "UT" or "GMT0" or similar. If yes then ok else return null. – Meno Hochschild Oct 27 '15 at 18:41

5 Answers5

6

java.time.ZoneId

The old date-time classes are now legacy, supplanted by the java.time classes.

Instead of java.util.TimeZone, you should be using ZoneId. For parsing a proper time zone name, call ZoneId.of.

ZoneId.of ( "Africa/Casablanca" ) 

Trap for ZoneRulesException

If the name you pass in unrecognized, the method throws a ZoneRulesException.

So, to catch a misspelling of Asia/Tokyo as Asia/Toyo, trap for the ZoneRulesException.

ZoneId z;
try {
    z = ZoneId.of ( "Asia/Toyo" );  // Misspell "Asia/Tokyo" as "Asia/Toyo".
} catch ( ZoneRulesException e ) {
    System.out.println ( "Oops! Failed to recognize your time zone name. ZoneRulesException message: " + e.getMessage () );
    return;
}
ZonedDateTime zdt = ZonedDateTime.now ( z );

See this code run live at IdeOne.com.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
3

Instead of iterating you could also simply use:

boolean exists = Arrays.asList(TimeZone.getAvailableIDs()).contains("Asia/Toyo");

or:

boolean exists = Arrays.stream(TimeZone.getAvailableIDs()).anyMatch("Asia/Toyo"::equals);

If you need to do that often, putting the available IDs in a HashSet would be more efficient.

assylias
  • 321,522
  • 82
  • 660
  • 783
1

This should answer your need TimeZone validation in Java

String[] validIDs = TimeZone.getAvailableIDs();
for (String str : validIDs) {
      if (str != null && str.equals("yourString")) {
        System.out.println("Valid ID");
      }
}
Community
  • 1
  • 1
Florian
  • 699
  • 1
  • 5
  • 19
0

Just check the returned TimeZone:

public static TimeZone getTimeZone(String timeZoneID) {
    TimeZone timeZone = TimeZone.getTimeZone(timeZoneID);
    if (timeZone.getID().equals(timeZoneID))
        return timeZone;
    throw new IllegalArgumentException("Unknown time zone: " + timeZoneID);
}

In Java 8, use the new ZoneId.of(String zoneId), which will fail for unknown time zones, and TimeZone.getTimeZone(ZoneId zoneId) if you need the old TimeZone:

ZoneId zoneId = ZoneId.of("Asia/Toyo");
TimeZone timeZone = TimeZone.getTimeZone(zoneId);
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 1
    The [documentation for TimeZone](http://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html) explicitly points out that the returned object may have an ID string different from what you request, such as `TimeZone.getTimeZone("GMT-8")`. – VGR Oct 27 '15 at 16:57
  • @VGR True, but the other answers, which search the available IDs, will fail for the same reason. A non-standard ID will be changed, and will not be in the list. As such, this is a better, yet still imperfect, solution. – Andreas Oct 27 '15 at 17:01
0

If you don't want to iterate over TimeZone.getAvailableIDs(). You can use.

boolean validateTimeZone(String timeZoneStr) {
    return TimeZone.getTimeZone(timeZoneStr).getID().matches(timeZoneStr);
}
User45
  • 74
  • 1
  • 10
  • This fails for time zone IDs with a plus (`+`) in them such as `Etc/GMT+2` since `matches()` interprets its argument as a regular expression, and the plus has a special meaning in regex. – Ole V.V. Jan 28 '23 at 14:40