--- Edited after a response indicating that the developer is using IBM J9 1.5.0 Java Virtual Machine ---
The IBM J9 JVM seems to have several errors and incompatibilities in the DateFormat syntax procedure that SimpleDateFormat inherits because it is a subclass of DateFormat. Some evidence that the IBM J9 is not working as you might expect, other JVMs (like Sun HotSpot JVM) can be seen here .
Note that these errors and incompatible elements are not even consistent in the J9 JVM, in other words, the IBM J9 formatting logic can actually generate formatted times that are incompatible with the IBM J9 parsing logic.
It seems that people tied to the IBM J9 JVM tend to work on a bug in the JVM without using DateFormat.parse (...) (or SimpleDateFormat.parse (...)). Instead, they typically use java.util.regex.Matcher to parse the fields manually.
Perhaps a later version of the JVM JVM fixes the problem, maybe not.
--- Original post follows ---
Funny, the same code is changed to:
import java.util.Date; import java.util.TimeZone; import java.text.SimpleDateFormat; import java.text.DateFormat; import java.text.ParseException; public class FormatsTest { public void testParse() throws ParseException { DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss.SSS Z"); formatter.setTimeZone(TimeZone.getDefault()); formatter.setLenient(false); System.out.println(formatter.format(new Date())); formatter.parse(formatter.format(new Date())); } public static void main(String[] args) throws Exception { FormatsTest test = new FormatsTest(); test.testParse(); } }
works great on my system. I would say that this is something in your environment. Either you compile the code on one release of the JVM major and run it on another (which may cause some problems, because the libraries may be outdated), or the system on which you run it may report time zone information strangely.
Finally, you might wonder if you are using a very early version of the JVM. Sometimes errors come in different versions, and they are fixed in later releases. Could you change your question to include information on "java -version"?
In any case, both of them are only educated guesses. The code should work as it is written.
Edwin buck
source share