Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6609721
Votes 0
Synopsis [Fmt-Da] java.text.SimpleDateFormat.format result some time not correct
Category java:classes_text
Reported Against
Release Fixed
State 11-Closed, duplicate of 4264153, bug
Priority: 4-Low
Related Bugs
Submit Date 26-SEP-2007
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  customer  for all thread. Don’t try to use SimpleDateFormat  customer  from multiple thread.
Posted Date : 2007-09-26 14:58:10.0
Work Around
N/A
Evaluation
java.text.Format subclasses are not thread-safe.
Posted Date : 2007-10-16 06:53:32.0
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang