Java time checking

I have a string, I need to check if this is a standard time zone identifier or not. I'm not sure which method I need to use.

String timeZoneToCheck = "UTC"; 

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

+8
java timezone validation
source share
8 answers

You can get all supported identifiers 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"); } } 
+10
source share

You can write it in one line

 public boolean validTimeZone(String timezone) { return Arrays.asList(TimeZone.getAvailableIDs()).contains(timezone); } 
+7
source share

I would suggest the following 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 a result, if the time zone identifier is incorrect or only the custom time zone is incorrect, you will get null . In addition, you can enter the appropriate null value handler (use case) - throw an exception, etc.

+4
source share

Since TimeZone#getTimeZone(String id) returns the default value if the value is invalid (instead of returning an invalid time zone), if the overridden value does not match the initial value, it is invalid.

 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); } 
+2
source share

If TimeZone.getAvailableIDs() contains an identifier, it is valid:

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

Unfortunately, the TimeZone.getTimeZone() method silently discards invalid identifiers and instead returns GMT:

Return:

the specified TimeZone or GMT if the given identifier cannot be understood.

+1
source share

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

 for (String str : TimeZone.getAvailableIDs()) { if(str.equals("UTC")){ //found } } 
+1
source share
 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 time zones:

 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")); 
+1
source share

This is a simpler solution than scrolling through all possible identifiers. It checks if the output is the default time zone (GMT), and if it was the actual user input.

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

Or if you want to use a valid time zone without calling getTimeZone twice:

 TimeZone myCurrentTimeZone; TimeZone temp = TimeZone.getTimeZone(input); if(input.equals("GMT") || !temp.getID().equals("GMT")) myCurrentTimeZone = temp; //valid timezone set //else // System.out.println("Invalid timezone."); 
0
source share

All Articles