1 public static final FastDateFormat ISO_DATE_FORMAT  = FastDateFormat.getInstance("yyyy-MM-dd"); 

上面的final 字段代表一个不可变的FastDateFormat,然而要让FastDateFormat字段真正的不可变,FastDateFormat内部必须遵循相应的规则才可以。

不要提供能修改对象状态的方法确保类不会被继承 
让所有字段都成为static final字段 
确保所有可变的组件不能被访问

 
其实DateFormateUtils内部细节实现完全依靠FastDateFormat
FastDateFormat这个类编写比较复杂,它有自己的一套解析规则,同时又使用了DateFormat类的一些规则,如Pattern。与SimpleDateFormat不同,FastDateFormat是线程安全,所以这个类在多线程的服务环境中特别有用。虽然它们都继承自java.text.Format,其实FastDateFormat相当于DateFormat与SimpleDateFormat的合并,只是功能更强大而已。
 

格式化日期

问题提出:SimpleDateFormat是非线程安全的,而您又需要一个ISO格式的日期。
解决方法:使用FastDateFormat或者使用DateFormatUtils提供的静态FastDateFormat实例,它提供了一些格式化日期的线程安全的方法。

1 Date now = new Date();
2 String isoDT = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(now);
3 System.out.println("It is currently: " + isoDT);

结果为:It is currently: 2014-07-02T14:38:26+08:00

如果您想使用传统的格式化类型,可以用FastDateFormat来代替SimpleDateFormat

FastDateFormat formatter =
new FastDateFormat( "yyyy-mm",
TimeZone.getDefault( ),
Locale.getDefault( ) ); String output = formatter.format( new Date( ) ); System.out.println(output); FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss").format(new Date());

在最新的版本中FastDateFormat的构造函数已经改为protected,统一使用getInstance()来获取FastDateFormat实例,上述的输出为2014-07.

FastDateFormat的更多相关文章

  1. 使用FastDateFormat来代替JDK自带的DateFormat

    之前一直使用SimpleDateFormat来做Date到String的类型转换,现建议使用apache commons-lang3中的FastDateFormat. 因为JDK里自带的SimpleD ...

  2. FastDateFormat日期工具类

    原文:https://blog.csdn.net/u013823538/article/details/80197476 public class DateUtil { public static f ...

  3. 日期格式化:SimpleDateFormat【线程不安全】、FastDateFormat和Joda-Time【后两个都是线程安全】

    SimpleDateFormat是线程不安全的,不能多个线程公用.而FastDateFormat和Joda-Time都是线程安全的,可以放心使用. SimpleDateFormat是JDK提供的,不需 ...

  4. 自己动手写文件查找,字符串查找,查询jar包等工具

    文件查找——搜索当前目录下的文件 知道大概的文件名称,使用 findf FileName findf.py import argparse, re, os from os.path import jo ...

  5. Spring单例模式与线程安全

    问题背景 这段时间在做项目的时候,考虑到Spring中的bean默认是单例模式的,那么当多个线程调用同一个bean的时候就会存在线程安全问题.如果是Spring中bean的创建模式为非单例的,也就不存 ...

  6. 【转】apache DateFormatUtils 与 DateUtils 的使用

    在Apache Commons项目的Lang里面,有两个类:DateUtils和DateFormatUtils,专门用于处理时间日期转换.它们在 org.apache.commons.lang.tim ...

  7. 让时间处理简单化 【第三方扩展类库org.apache.commons.lang.time】

    JAVA的时间日期处理一直是一个比较复杂的问题,大多数程序员都不能很轻松的来处理这些问题.首先Java中关于时间的类,从 JDK 1.1 开始,Date的作用很有限,相应的功能已由Calendar与D ...

  8. 转-Spring单例模式与线程安全

    问题背景 这段时间在做项目的时候,考虑到Spring中的bean默认是单例模式的,那么当多个线程调用同一个bean的时候就会存在线程安全问题.如果是Spring中bean的创建模式为非单例的,也就不存 ...

  9. 有关Java的日期处理的一些杂记

    在企业应用开发中,经常会遇到日期的相关处理,说实话JDK自带的日期方法很难用.就我个人而言我一般都会采用joda-time来替代JDK自身的日期. 这篇文章是杂记,所以写的比较零散,希望大家不要见怪. ...

随机推荐

  1. Gcc的Makefile简单使用

    Gcc的Makefile简单使用http://blog.chinaunix.net/uid-9330295-id-2425867.html

  2. mapReduce的shuffle过程

    http://www.jianshu.com/p/c97ff0ab5f49 总结shuffle 过程: map端的shuffle: (1)map端产生数据,放入内存buffer中: (2)buffer ...

  3. Linux下安装流量监控工具iftop

    在Linux系统中,top命令可以查看系统资源包括内存,CPU占用信息,查看和探测网络状态可以使用netstat,nmap等工具,实时流量监控可以使用iftop,下面是在CentOS7系列系统上安装i ...

  4. Linux安装mariadb二进制版本

    上一篇说了mariadb编译安装过程,但在生产环境中一般使用发布好的二进制版本,由于安装过程和之前一样,不再详细叙述,只是简单概括一下安装过程: 1. 下载 地址为:https://downloads ...

  5. repo 修改邮箱地址

    需要重新运行 repo init 被带上参数: --config-name xx@a.com

  6. druid配置数据库连接使用密文密码

    spring使用druid配置dataSource片段代码 dataSource配置 <!-- 基于Druid数据库链接池的数据源配置 --> <bean id="data ...

  7. ios .a和.framework

    创建Aggregate来合并模拟器和真机通用的framework 然后在Build Phases下New Run Script Phase创建合并脚本: # Constants SF_TARGET_N ...

  8. 1Z0-050

    QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...

  9. 【leetcode】Remove Duplicates from Sorted List

    题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  10. F#之旅4 - 小实践之快排

    参考文章:https://swlaschin.gitbooks.io/fsharpforfunandprofit/content/posts/fvsc-quicksort.html F#之旅4 - 小 ...