两种取日期格式字符串的对比较
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的这种方式简便一些。
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的这种方式简便一些。
相关文章推荐
- Java:日期字符串格式大小比较
- 字符串格式参数的日期比较函数
- 比较一个字符串日期和一个日期格式的日期的大小
- Python格式字符串两种方法比较
- Java:日期字符串格式大小比较
- 计算两日期相差天数(用字符串格式的时间形式来比较)、将时间戳转为字符串,开始到结束的时间差(用时间戳来比较)
- 字符串格式的日期比较大小
- 字符串格式参数的日期比较函数
- 字符串格式参数的日期比较函数
- 字符串格式参数的日期比较函数
- js 日期比较 (输入的是字符串格式)
- 字符串格式参数的日期比较函数
- 正则表达式:日期,电话,邮箱等常用字符串;js中日期的带下的比较,获取不同格式的日期
- javascript中怎样将字符串“04/06/2011”的日期加1天,返回成字符串“04/06/2011”格式的字符串
- ORA-01830: 日期格式图片在转换整个输入字符串之前结
- C#语言之“string格式的日期时间字符串转为DateTime类型”的方法
- 【转】MS Sql server 日期转换为特定字符串格式
- 关于java.sql.SQLException: ORA-01830: 日期格式图片在转换整个输入字符串之前结束
- Oracle修改时间报:ORA-01830: 日期格式图片在转换整个输入字符串之前结束的解决办法
- GDI中获取字符串精确绘制范围的两种做法比较