/** * Class that contains string contants that can be used in other classes. * */ public final class Constants {
// Logging messages public final static String TRACK = "FCLPLM000000"; public final static String METHOD_BEGIN = "in"; public final static String METHOD_END = "out"; public final static String METHOD_INSIDE = "inside";
// Exception messages public final static String ENVIRONMENT_FILE_NOT_FOUND = "FCLEAI000001"; public final static String ENVIRONMENT_FIELD_NOT_FOUND = "FCLEAI000002";
When I run the above code on AIX 5.2 machine with JDK 1.4.2, I get the following output: wasusr02@UIT-MCP-WEB8:/data/was5-2#/appl/was51/java/bin/java TestSimpleDateFormat Inside Try Before formating Today: 18/10/2007 Inside exception java.lang.ExceptionInInitializerError at TestSimpleDateFormat.main(TestSimpleDateFormat.java:33) Caused by: java.lang.IllegalArgumentException: Illegal pattern character 'T' at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:700) at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:513) at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:462) at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:443) at Constants.<clinit>(Constants.java:27) ... 1 more wasusr02@UIT-MCP-WEB8:/data/was5-2#
The following line in the code: String strDateConst = Constants.staffwareDateFormatter.format(todayDate); causes the Throwable error.
When I run the same on AIX 5.1 machine with JDK 1.3.1 , everything works fine.
Can anyone please throw some light on what is happening? Are there any JDK dependencies?
Thanks in Advance, Anup [ October 18, 2007: Message edited by: Anup Bansal ]
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
7
posted
0
That an exception is thrown is correct, as "T" is not a valid pattern character (if you want to describe the literal character, enclose it with single quotes, as the javadocs mention).
Offhand I'd say that the 1.3.1 JVM has a bug that got fixed in the 1.4.2 JVM.
I am not sure where this 'T' comes from and thats what I want to know. Why am I getting such a wierd error?
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
7
posted
0
It's right in the middle of the xmlTimeStampFormatter pattern.
Anup Bansal
Ranch Hand
Joined: Sep 12, 2006
Posts: 69
posted
0
Thanks! I changed it as: public final static SimpleDateFormat xmlTimeStampFormatter = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss"); and it worked.
However I still have one doubt. In the InitalProcessor class, I an using staffwareDateFormatter from the Constants class and not xmlTimeStampFormatter. Then why do I get the error specific to xmlTimeStampFormatter?
bart zagers
Ranch Hand
Joined: Feb 05, 2003
Posts: 234
posted
0
When you first use the Constants class, it is loaded by the classloader and all the static variables are instantiated. During this instantiation the exception occurs during the construction of the xmlTimeStampFormatter.
As a sidenote, it is usually not a good idea to create static SimpleDateFormat objects in a Constants class, (unless you really do it deliberately). SimpleDateFormat is not threadsafe and you would not be the first one to get bitten by this. We usually only put the actual format Strings in such a Constants class.
Anup Bansal
Ranch Hand
Joined: Sep 12, 2006
Posts: 69
posted
0
Thanks for the info. I did not place the complete code.The code that I have put in the call is just a snippet. Appologies for the same. Please find the complete code for IntialProcessor below: public class InitialProcessor{
Now the output when the run this with T without the single quotes on AIX 5.2 with JDK 1.4.2 is: wasusr02@UIT-MCP-WEB8:/data/was5-2#\^J/appl/was51/java/bin/java TestSimpleDateFormat Main: in Inside Try Before formating Today: 18/10/2007 convertDate: in Inside exception java.lang.ExceptionInInitializerError at TestSimpleDateFormat.convertDate(TestSimpleDateFormat.java:46) at TestSimpleDateFormat.main(TestSimpleDateFormat.java:32) Caused by: java.lang.IllegalArgumentException: Illegal pattern character 'T' at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:700) at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:513) at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:462) at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:443) at Constants.<clinit>(Constants.java:27) ... 2 more wasusr02@UIT-MCP-WEB8:/data/was5-2#
Now as you can see, I am using Constants class at the very first statement in the main. As per the expalnation you provided, it should fail at this statement. However if you check the output, it fails only when I try to access the staffwareDateFormatter in the method convertDate.
This is what I fail to understand. Do you have any idea??
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
I thought you fixed the T error by adding ' around the T. Did you remove them again, to see what would happen?
Constants.METHOD_BEGIN is a compile-time constant. As is Constants.METHOD_END. This means it's possible to evaluate it completely at compile time, and classes that need its value get that value substituted in at compile time, not run time. So at run time, when InitialProcessor.main() runs, the class Constants does not need to be loaded & initialized until you get to the line that uses Constants.staffwareDateFormatter. [ October 18, 2007: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Anup Bansal
Ranch Hand
Joined: Sep 12, 2006
Posts: 69
posted
0
Thanks for all the help! Yes I had fixed the issue with adding ' around T. I was curious as to why it did not fail the first time Constants class was being used and so had posted the earlier question. I have the answer now. Thanks!!!
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Does SimpleDateFormat have any dependency on JDK versions