aspose file tools*
The moose likes Java in General and the fly likes Does SimpleDateFormat have any dependency on JDK versions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Does SimpleDateFormat have any dependency on JDK versions" Watch "Does SimpleDateFormat have any dependency on JDK versions" New topic
Author

Does SimpleDateFormat have any dependency on JDK versions

Anup Bansal
Ranch Hand

Joined: Sep 12, 2006
Posts: 69
Hi All,

Does SimpleDateFormat have any dependency on JDK versions?
I am encountering the following issue:

I have a java file named as Constants.java. The contents of this file is as follows:
package com.test.util;

import java.text.SimpleDateFormat;

/**
* 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";

public final static SimpleDateFormat staffwareDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
public final static SimpleDateFormat stafwfareTimeFormatter = new SimpleDateFormat("HH:mm");
public final static SimpleDateFormat xmlTimeStampFormatter = new SimpleDateFormat("yyyy/MM/ddTHH:mm:ss");
}



This class is being referenced by another class InitialProcessor.java
Given below is a code snippet of the same:


package com.test.processor;

import java.math.BigDecimal;
import java.util.*;

import com.test.util.Constants;

public class InitialProcessor{

public static void main(String[] args) {

try {
System.out.println("Inside Try");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date todayDate = new Date();
System.out.println("Before formating");
String strDate = dateFormat.format(todayDate);
System.out.println("Today: " + strDate);
String strDateConst = Constants.staffwareDateFormatter.format(todayDate);
System.out.println("Today from Const: " + strDateConst);
System.out.println("After formating");

} catch (Throwable e){
System.out.println("Inside exception");
e.printStackTrace();

}


}

}



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: 41815
    
  62
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.


Ping & DNS - my free Android networking tools app
Anup Bansal
Ranch Hand

Joined: Sep 12, 2006
Posts: 69
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: 41815
    
  62
It's right in the middle of the xmlTimeStampFormatter pattern.
Anup Bansal
Ranch Hand

Joined: Sep 12, 2006
Posts: 69
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
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
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{

public static void main(String[] args) {

try {
System.out.println("Main: " + Constants.METHOD_BEGIN);
System.out.println("Inside Try");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date todayDate = new Date();
System.out.println("Before formating");
String strDate = dateFormat.format(todayDate);
System.out.println("Today: " + strDate);
String strDateConst = convertDate (todayDate);
System.out.println("Today from Const: " + strDateConst);
System.out.println("After formating");
System.out.println("Main: " + Constants.METHOD_END);

} catch (Throwable e){
System.out.println("Inside exception");
e.printStackTrace();
}
}

private static String convertDate (Date todayDate){
System.out.println("convertDate: " + Constants.METHOD_BEGIN);
String strConvertedDate = Constants.staffwareDateFormatter.format(todayDate);
System.out.println("convertDate: " + Constants.METHOD_BEGIN);
return strConvertedDate;
}
}

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: 18671
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
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://aspose.com/file-tools
 
subject: Does SimpleDateFormat have any dependency on JDK versions