Word-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
public class TestDateFormat
{
PRivate SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public void method1()
{
sdf.format(new Date());
}
public void method2()
{
sdf.format(new Date());
}
)
为了渐少new 的次数而把SimpleDateFormat做成成员或者静态成员,但这样的做法是隐含着错误的,是不 import
java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class TestDateFormat
{
private SimpleDateFormat sdf ;
public static void main(String[] args)
{
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date1 = new Date();
Date date2 = new Date(date1.getTime()+1000*60*60*24);
System.out.println(date1);
System.out.println(date2);
Thread thread1 = new Thread(new Thread1(sdf,date1));
thread1.start();
Thread thread2 = new Thread(new Thread2(sdf,date2));
thread2.start();
}
}
class Thread1 implements Runnable
{
private SimpleDateFormat sdf;
private Date date;
public Thread1(SimpleDateFormat sdf,Date date)
{
this.sdf = sdf;
this.date = date;
}
public void run()
{
for(;;)
{
String strDate = sdf.format(date);
if("2007-02-27".equals(strDate))
{
System.err.println("Error: date1="+strDate);
System.exit(0);
}
}
}
}
class Thread2 implements Runnable
{
private SimpleDateFormat sdf;
private Date date;
public Thread2(SimpleDateFormat sdf,Date date)
{
this.sdf = sdf;
this.date = date;
}
public void run()
{
for(;;)
{
String strDate = sdf.format(date);
if("2007-02-26".equals(strDate))
{
System.err.println("Error: date1="+strDate);
System.exit(0);
}
}
}
}