title: NumberFormatException while parsing date with SimpleDateFormat.parse()
body: <p>Have a function that creates a time-only <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Date.html" rel="noreferrer">Date</a> object. (why this is required is a long story which is irrelevant in this context but I need to compare to some stuff in XML world where TIME (i.e. time-only) is a valid concept). </p>
  
  <pre><code>private static final SimpleDateFormat DF_TIMEONLY = new SimpleDateFormat("HH:mm:ss.SSSZ");    
  
  public static Date getCurrentTimeOnly() {
  
      String onlyTimeStr = DF_TIMEONLY.format(new Date());  // line #5
      Date  onlyTimeDt = null;
      try {
          onlyTimeDt = DF_TIMEONLY.parse(onlyTimeStr);  // line #8
      } catch (ParseException ex) { 
          // can never happen (you would think!)
      }
      return onlyTimeDt;
  }
  </code></pre>
  
  <p>There are probably at least a couple other ways to create a time-only Date in Java (or more precisely one where the date part is 1970-01-01) but my question is really not about that.</p>
  
  <p>My question is that this piece of code starts randomly throwing <strong>NumberFormatException</strong> on line #8 after having run in production for long time. Technically I would say that this should be impossible, right ?</p>
  
  <p>Here's an extract of random NumberFormatExceptions that come from above piece of code:</p>
  
  <pre><code>java.lang.NumberFormatException: multiple points
  java.lang.NumberFormatException: For input string: ".11331133EE22"
  java.lang.NumberFormatException: For input string: "880044E.3880044"
  java.lang.NumberFormatException: For input string: "880044E.3880044E3"
  </code></pre>
  
  <p>First of all I hope we can agree that formally this should be impossible?  The code uses the same format (<code>DF_TIMEONLY</code>) as output and then input. Let me know if you disagree that it should be impossible.</p>
  
  <p>I haven't been able to re-produce the problem in a standalone environment. The problem seems to come when the JVM has run for a long time (>1 week). I cannot find a pattern to the problem, i.e. summer time / winter time, AM/PM, etc.  The error is sporadic, meaning that one minute it will throw NumberFormatException and the next minute it will run fine.</p>
  
  <p>I suspect that there's some kind of arithmetic malfunction somewhere in either the JVM or perhaps even in the CPU. The above exceptions suggests that there's floating point numbers involved but I fail to see where they would come from. As far as I know Java's Date object is a wrapper around a <code>long</code> which holds the number of millis since the epoch.</p>
  
  <p>I'm guessing what is happening is that there's an unexpected string <code>onlyTimeStr</code> created in line #5 so the problem really lies here rather than in line #8.</p>
  
  <p>Here's an example of a full stacktrace:</p>
  
  <pre><code>java.lang.NumberFormatException: For input string: "880044E.3880044E3"
      at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
      at java.lang.Double.parseDouble(Double.java:540)
      at java.text.DigitList.getDouble(DigitList.java:168)
      at java.text.DecimalFormat.parse(DecimalFormat.java:1321)
      at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2086)
      at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1455)
      at java.text.DateFormat.parse(DateFormat.java:355)
      at org.mannmann.zip.Tanker.getCurrentTimeOnly(Tanker.java:746)
  </code></pre>
  
  <p>Environment:  Java 7</p>