JDK-6609721 : [Fmt-Da] java.text.SimpleDateFormat.format result some time not correct
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-09-26
  • Updated: 2010-07-29
  • Resolved: 2007-10-16
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Window2000, Window XP, Linux

A DESCRIPTION OF THE PROBLEM :
java.text.SimpleDateFormat.format(java.util.Date) method is inconsistent. Means get error some time in multithreaded application.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 static {dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));}

java.util.Date dt = // Create Date with Value 2006-11-08 11:27:00

dateFormat.format(dt);


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
2006-11-08 11:27:00
ACTUAL -
2006-11-08 11:27:45

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Error 2006-11-08 11:27:00: Error Message -java.lang.Exception: new vale 2006-11-08 11:27:45 is differs with from2006-11-08 11:27:00
	at org.ets.test.TestDateFormat$NewThread.run(TestDateFormat.java:57)
	at java.lang.Thread.run(Unknown Source)

REPRODUCIBILITY :
This bug can be reproduced rarely.

---------- BEGIN SOURCE ----------
package org.ets.test;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author Motilal Patel, ETS.org
 *
 */
public class TestDateFormat {
	  private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  static {dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));}
	  
	  public static void main(String[] args) {
		int size = 20000;
		String[] arrValue = new String[size];
		Date[] arrDate = new Date[size];
		for (int i = 0; i < arrValue.length; i++) {
			arrDate[i] = new Date(System.currentTimeMillis()-(i*63642355*555888*8888*90999)-(i*6666*6666*6668));
			arrValue[i] = dateFormat.format(arrDate[i]);
		}
		
		NewThread[] arrTh = new NewThread[size];
		for (int i = 0; i < arrTh.length; i++) {
			arrTh[i] = new NewThread(arrValue[i],arrDate[i]);
		}
			
		try  {
				for (int i = 0; i < arrTh.length; i++) {
					arrTh[i].t.join();
				}
			}
			catch (InterruptedException e) {
				System.out.println("main thread interrupted.");
			}
			System.out.println("Done...");
		}
		
		
		static class NewThread implements Runnable {
			String value;
			Date dt;
			Thread t;
			NewThread(String value,Date dt) {
				this.value=value;
				this.dt = dt;
				t=new Thread(this, value);
				t.start();
			}

			public void run() {
				try
				{
				   String newValue = dateFormat.format(this.dt);
				   if(!newValue.equals(value))
				   {
					   throw new Exception ("new vale "+newValue+" is differs with from" + value);
				   }
				    
				} catch(Exception ex){
					StringWriter sw = new StringWriter();
			        ex.printStackTrace(new PrintWriter(sw));
			        String error = sw.toString();
			        System.out.println(" Error " + value+
			   				": Error Message -" +error);
				}
			}
		}
	
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Don���t use static member for SimpleDateFormat. Create new SimpleDateFormat object for all thread. Don���t try to use SimpleDateFormat object from multiple thread.

Comments
EVALUATION java.text.Format subclasses are not thread-safe.
16-10-2007