2021年3月26日星期五

Some Time Zones return IllegalArgumentException

This code example uses 3 time zones (EST, PST, EET). For each Time Zone, a Date object is created and the toString() is run to print out the format being used. Then this same String value is passed to a Constructor and used to create a new Date Object. The code does run a check to ensure the Time Zone being used is valid.

All 3 Time Zones (EST, PST, EET) are valid but when creating the object, the java.lang.IllegalArgumentException is returned only for EET.

import java.util.*;  import java.text.*;    public class DateTest  {       public static void main (String[] args)     {                  System.out.println("=======Test 1 : using EST=======");         isValidTimeZone("EST");         TimeZone.setDefault(TimeZone.getTimeZone("EST"));         runTest();                  System.out.println("=======Test 2 : using PST=======");         isValidTimeZone("PST");         TimeZone.setDefault(TimeZone.getTimeZone("PST"));         runTest();           System.out.println("=======Test 3 : using EET=======");         isValidTimeZone("EET");         TimeZone.setDefault(TimeZone.getTimeZone("EET"));         runTest();              }     private static void isValidTimeZone(String tz)     {         String[] validIDs = TimeZone.getAvailableIDs();         boolean validTZ = false;         for (String str : validIDs) {               if (str != null && str.equals(tz)) {                 validTZ = true;                 break;               }         }                  if (validTZ)         {             System.out.println(tz + " is a Valid Time Zone");         }         else         {             System.out.println(tz + " is **NOT** a Valid Time Zone");         }     }          private static void runTest()     {         try         {          String myDateString = new Date().toString();          System.out.println("     Default Date String : " + myDateString);          MyObjectWithADate myObject = new MyObjectWithADate(new Date(myDateString));         }         catch(Exception e)         {             System.out.println("     Object NOT Created!!!!!");            e.printStackTrace(System.out);         }       }  }        public MyObjectWithADate (Date eventDate)      {          System.out.println("     Passed in Date :      " + eventDate.toString());  //      this.eventDate = eventDate;          try {              this.eventDate = DateFormat.getInstance().parse(eventDate.toString());              System.out.println("     Object Created");          } catch (ParseException e) {              System.out.println("     Object NOT Created");              e.printStackTrace();          }                }  }     

Here is the output.

enter image description here

Based on the Java 11 docs, it does comment that Date is deprecated and that DateFormat.parse() should be used.

As a test, the code for the Object was modified to use DateFormat.parse but this only made matters worse.

    public MyObjectWithADate (Date eventDate)      {          System.out.println("     Passed in Date :      " + eventDate.toString());   //     this.eventDate = eventDate;          try {              this.eventDate = DateFormat.getInstance().parse(eventDate.toString());              System.out.println("     Object Created");          } catch (ParseException e) {              System.out.println("     Object NOT Created");              e.printStackTrace();          }                }  }     

Here are the new results.

enter image description here

Questions 1 : From which environment variable does the JVM obtain the timezone?

Questions 2 : Using the original code, why does the Exception occur when it is using the same format as the one provided by the JVM?

Questions 3 : What specifically is it about EET that causes it to fail yet EST and PST and be swapped without issues?

Questions 4 : If I want to allow the original code to be run by anyone in any Time Zone, what needs to be changed?

EDITED TO ADD THE FOLLOWING :

The above code is a scaled down model. Unfortunately the actual code can not be modified in all places to change from using the Date object.

I did run another test using the SimpleDateFormat within the MyObjectWithADate object. This once again works for the EST and PST but not the EET.

class MyObjectWithADate   {          private Date        eventDate;                    public MyObjectWithADate (Date eventDate)          {              System.out.println("     Passed in Date :      " + eventDate.toString());              String datePattern = new String ("E MMM dd HH:mm:ss z yyyy");              SimpleDateFormat simpleDateFormat = new SimpleDateFormat(datePattern);              try {                  this.eventDate = simpleDateFormat.parse(eventDate.toString());                  System.out.println("     Object Created");              } catch (ParseException e) {                  System.out.println("     Object NOT Created");                  e.printStackTrace();              }              System.out.println("     Object Created");          }  }     

I am starting to think that my original question should have been, how to take the following String

Fri Mar 26 21:42:52 EET 2021

and place it into a Date object.

https://stackoverflow.com/questions/66821979/some-time-zones-return-illegalargumentexception March 27, 2021 at 01:51AM

没有评论:

发表评论