使用FastDateFormat来代替JDK自带的DateFormat

2014-10-08 14:58

SimpleDateFormat来做Date到String的类型转换,建议使用Apache commons-lang中的FastDateFormat。

因为JDK里自带的SimpleDateFormat存在线程不安全问题。

maven依赖:

  1. <dependency>  
  2.     <groupId>commons-lang</groupId>  
  3.     <artifactId>commons-lang</artifactId>  
  4.     <version>2.5</version>  
  5. </dependency>  
代码:

  1. private String initDate() {  
  2.        Date d = new Date();  
  3.        FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");  
  4.        return fdf.format(d);  
  5.    } 
^