微信扫一扫

028-83195727 , 15928970361
business@forhy.com

使用FastDateFormat来代替JDK自带的DateFormat

2016-08-02

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

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

maven依赖:

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.5</version>
</dependency>
代码:

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