public class Test {
//private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static Map<String,DateFormat> cache = new HashMap<String,DateFormat>();
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
for(int i=0;i<=10;i++){
formate(new Date());
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime);
}
public static String formate(Date date){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这里是否每次都生成一个新的对象,换成全局的效率比这个高,具体原因是不是这个?
return df.format(date);
}
}问题在注释中!
//private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static Map<String,DateFormat> cache = new HashMap<String,DateFormat>();
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
for(int i=0;i<=10;i++){
formate(new Date());
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime);
}
public static String formate(Date date){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这里是否每次都生成一个新的对象,换成全局的效率比这个高,具体原因是不是这个?
return df.format(date);
}
}问题在注释中!
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date).toString();
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df);//十次循环输出的是同一地址
System.out.println(new Test());//十次循环输出的是十个不同的地址
return df.format(date).toString();
} 上面的是什么原因呢?toString()方法也没有被重载过。
在多线程情况下,问题突出。