Why am I getting a ParseException when using SimpleDateFormat to format a date and then parse it?

I was debugging some existing code for which unit tests do not work on my system, but not on colleagues' systems. The main reason is that SimpleDateFormat throws ParseExceptions when parsing dates to be processed. I created a unit test that demonstrates code that does not work on my system:

import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; import junit.framework.TestCase; public class FormatsTest extends TestCase { public void testParse() throws ParseException { DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss.SSS Z"); formatter.setTimeZone(TimeZone.getDefault()); formatter.setLenient(false); formatter.parse(formatter.format(new Date())); } } 

This test throws a ParseException on my system, but works successfully on other systems.

 java.text.ParseException: Unparseable date: "20100603100243.118 -0600" at java.text.DateFormat.parse(DateFormat.java:352) at FormatsTest.testParse(FormatsTest.java:16) 

I found that I can setLenient(true) and the test will succeed. setLenient(false) is what is used in production code that mimics this test, so I don't want to change it.

+7
java ibm simpledateformat
source share
3 answers

--- 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.

+6
source share

This could probably be a bug in the IBM J9 VM about the SimpleDateFormat class.

This post shows a similar problem and says that it should be fixed on v6.

You can find the changelog for several releases here .

I see there the number associated with DateFormat. Therefore, you should probably raise an error report or something with IBM so that they can give you a patch.

+1
source share

Check the LANG environment variable of your computer and the remote computer.

The date is parsed according to the locale, so "Jul" works like July only if your LANG is set to English, otherwise a ParseException will be thrown.

You can do a quick test by running export LANG="en_US.UTF-8" and then running your program.

You can also set the locale programmatically using the following method: DateFormat.getDateInstance (int, java.util.Locale)

0
source share

All Articles