Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>--- Edited after response indicating that the developer is using IBM's J9 1.5.0 Java Virtual Machine ---</p> <p>IBM's J9 JVM seems to have a few bugs and incompatibilities in the parse routine of DateFormat, which SimpleDateFormat likely inherits because it is a subclass of DateFormat. Some evidence to support that IBM's J9 isn't functioning quite the way you might expect other JVMs (like Sun's HotSpot JVM) can be seen <a href="http://jira.atlassian.com/browse/JRA-16371" rel="noreferrer">here</a>.</p> <p>Note that these bugs and incompatibilites are not even consistent within the J9 JVM, in other words, the IBM J9 formatting logic might actually generate formatted times that are not compatible with the IBM J9 parsing logic.</p> <p>It seems that people who are tied to IBM's J9 JVM tend to work around the bug in the JVM by not using DateFormat.parse(...) (or SimpleDateFormat.parse(...)). Instead they tend to use java.util.regex.Matcher to parse the fields out manually.</p> <p>Perhaps a later release of the J9 JVM fixes the issue, perhaps not.</p> <p>--- Original post follows ---</p> <p>Funny, the same code modified to:</p> <pre><code>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(); } } </code></pre> <p>runs fine on my system. I would wager that it is something in your environment. Either you are compiling the code on one JVM major release and running it on another (which can cause some issues as the libraries could be out of date) or the system you are running it on might be reporting the time zone information oddly.</p> <p>Finally, you might want to consider if you are using a very early point release of the JVM. Sometimes bugs do creep into the various versions, and they are fixed in later point releases. Could you please modify your question to include the "java -version" information for you system?</p> <p>Either way, both of these are just educated guesses. The code should work as written.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload