您的位置:首页 > 其它

两种取日期格式字符串的对比较

2008-01-24 16:29 253 查看
下面我用代码来说明问题:

private static SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ssss");
/**
* @param args
*/
public static void main(String[] args) {
testDateFormat();
}
public static void testDateFormat(){
long ll=System.currentTimeMillis();
int count=10000;
for(int i=0;i<count;i++){
Calendar calendar=Calendar.getInstance();
String date=Integer.toString(calendar.get(Calendar.YEAR))+"-"+
Integer.toString(calendar.get(Calendar.MONTH)+1)+"-"+
Integer.toString(calendar.get(Calendar.DAY_OF_MONTH))+" "+
Integer.toString(calendar.get(Calendar.HOUR_OF_DAY))+":"+
Integer.toString(calendar.get(Calendar.MINUTE))+":"+
Integer.toString(calendar.get(Calendar.SECOND))+"."+
Integer.toString(calendar.get(Calendar.MILLISECOND));
}
System.out.println(System.currentTimeMillis()-ll);
ll=System.currentTimeMillis();
for(int i=0;i<count;i++){
format.format(new Date());
}
System.out.println(System.currentTimeMillis()-ll);
}





其中执行一次的结果: 172 78 结果很明显效率高百分之五十以上 不过考虑到编写复杂程度,当然还是.format的这种方式简便一些。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: