Name: clC74495 Date: 06/08/98
Please run the following code:
import java.text.*;
import java.util.*;
import java.io.*;
class FormatTester
{
// Constants. Modify to your needs.
static String timeZoneID = "ECT";
static int THREADNUMBER = 3;
static int ITERATIONS = 1000;
// The format of the date preceeding all log file entries.
static SimpleDateFormat simpleDateFormat;
static PrintWriter log;
public static void main(String[] args)
{
TestThread[] testThreadArray = new TestThread[FormatTester.THREADNUMBER];
System.out.println("This is FormatTester.");
// Initialize date format and set up the correct time zone.
simpleDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(FormatTester.timeZoneID));
Date date = new Date();
String timeStamp = FormatTester.simpleDateFormat.format(date);
String logFileName = timeStamp + "-log.txt";
File logFile = new File(System.getProperty("user.dir"), logFileName);
FileOutputStream logFileOutputStream = null;
try
{
logFileOutputStream = new FileOutputStream(logFile);
}
catch (IOException e)
{
System.out.println("Cannot create log file. " + e);
System.out.println("Exiting.");
System.exit(1);
}
log = new PrintWriter(logFileOutputStream, true);
for (int i = 0; i < FormatTester.THREADNUMBER; i++)
{
testThreadArray[i] = new TestThread();
testThreadArray[i].start();
}
System.out.println("Up and running.");
}
}
class TestThread extends Thread
{
public void run()
{
for (int i = 0; i < FormatTester.ITERATIONS; i++)
{
Date date = new Date();
// Here it is!
String timeStamp = FormatTester.simpleDateFormat.format(date);
FormatTester.log.println(timeStamp);
}
}
}
The output includes lines like this:
19980607-010650 (correct)
19980607-00010650 (incorrect)
1998000607-010650 (incorrect)
1998060007-00010659 (incorrect)
(Review ID: 33204)
======================================================================
- relates to
-
JDK-4228335 SimpleDateFormat is not threadsafe (one more try)
-
- Closed
-