`
zyq8557
  • 浏览: 46402 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

FastDateFormat,DateFormatUtils

阅读更多

格式化日期

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

    Date now = new Date();
    String isoDT 
= DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(now);
    System.out.println(
"It is currently: " + isoDT);
结果为It is currently: 2006-03-08T10:44:29+08:00
如果您想使用传统的格式化类型,可以用FastDateFormat来代替SimpleDateFormat,
// create a formatter that simply prints the year and month

FastDateFormat formatter 
= 
    
new FastDateFormat( "yyyy-mm",
                         TimeZone.getDefault( ),
                         Locale.getDefault( ) );

String output 
= formatter.format( new Date( ) );

System.out.println(output);
在最新的版本中FastDateFormat的构造函数已经改为protected,统一使用getInstance()来获取FastDateFormat实例,上述的输出为2006-03.
Discussion:In fact, you should be aware that none of the Sun formatting classes are thread-safe. 以下是DateFormatUtils里面提供的Static date/time formats。

Name

Format

ISO_DATE_FORMAT

yyyy-MM-dd"2004-01-02"

ISO_DATE_TIME_ZONE_FORMAT

yyyy-MM-ddZZ"2004-01-02-07:00"

ISO_DATETIME_FORMAT

yyyy-MM-dd'T'HH:mm:ss"2004-01-02T23:22:12"

ISO_DATETIME_TIME_ZONE_FORMAT

yyyy-MM-dd'T'HH:mm:ssZZ"2004-01-02T21:13:45-07:00"

ISO_TIME_FORMAT

'T'HH:mm:ss"T04:23:22"

ISO_TIME_NO_T_FORMAT

HH:mm:ss"05:12:34"

ISO_TIME_NO_T_TIME_ZONE_FORMAT

HH:mm:ssZZ"12:32:22-07:00"

ISO_TIME_TIME_ZONE_FORMAT

'T'HH:mm:ssZZ"T18:23:22-07:00"

SMTP_DATETIME_FORMAT

EEE, dd MMM yyyy HH:mm:ss Z"Wed, 01 Feb 2004 20:03:01 CST"

分享到:
评论

相关推荐

    org.apache.commons.lang jar包下载(commons-lang3-3.1.jar)

    org.apache.commons.lang.time.DateFormatUtils.class org.apache.commons.lang.time.DateUtils$DateIterator.class org.apache.commons.lang.time.DateUtils.class org.apache.commons.lang.time....

    commons-lang.jar

    org.apache.commons.lang.time.DateFormatUtils.class org.apache.commons.lang.time.DateUtils$DateIterator.class org.apache.commons.lang.time.DateUtils.class org.apache.commons.lang.time....

    org.apache.commons.lang jar包下载

    org.apache.commons.lang.time.DateFormatUtils.class org.apache.commons.lang.time.DateUtils$DateIterator.class org.apache.commons.lang.time.DateUtils.class org.apache.commons.lang.time....

    Java SimpleDateFormat线程安全问题原理详解

    Date date = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss").parse("2020-05-10 19:53:00"); ... } } ``` 使用FastDateFormat可以确保日期时间格式化结果正确且线程安全。 在实际开发中,我们可以根据需要...

    commons-lang-2.4.jar

    `FastDateFormat`是比`SimpleDateFormat`更高效的日期格式化工具,减少了线程安全问题。 三、数学计算 `NumberUtils`提供了数学运算相关的辅助方法,如`NumberUtils.max()`和`NumberUtils.min()`可以找出一组数值...

    Apache Commons Lang 3.4_EN_API.rar

    4. **日期和时间**: `DateUtils`和`FastDateFormat`类分别提供了日期的解析、格式化和比较功能。`DateUtils`可以方便地进行日期的加减操作,`FastDateFormat`则是一种高效的日期格式化工具。 5. **反射工具**: `...

    commons-lang-2.3

    `DateUtils` 提供了日期的格式化、解析和比较功能,而 `FastDateFormat` 是一个高效的日期格式化器,比 Java 内置的 `SimpleDateFormat` 性能更优。 3. **对象操作**:`ClassUtils` 提供了关于类和对象的通用方法,...

    高并发之-SimpleDateFormat类的线程安全问题和解决方案.docx

    4. 使用池化技术: 可以使用池化技术,例如Apache Commons Lang中的FastDateFormat类,来池化SimpleDateFormat对象,从而避免了线程安全问题。 结论 SimpleDateFormat类的线程安全问题是由于它使用了缓存机制导致的...

    Java日历源码 JavaRili.rar

    在Java中,处理阴历需要额外的库,比如`org.apache.commons.lang3.time.FastDateFormat`或者第三方的`com.github.bingoohuang:chinatime`库,这些库可以帮助开发者方便地实现农历和阳历之间的转换。 在Eclipse环境...

    android 常用快速开发集成工具类

    7. **日期时间处理**:日期时间格式化、比较、转换等工具类,例如使用Java 8的LocalDateTime或者三脚猫FastDateFormat。 8. **异常处理**:全局异常捕获和处理,提供统一的日志记录和错误反馈机制。 9. **性能优化...

    java实现桌面精灵(java源代码)(一个日历)

    为了支持农历,我们需要引入第三方库,如`org.apache.commons.lang3.time.FastDateFormat`或`com.github.zengfr.conuniv.calendar`,这些库通常包含了转换公历与农历的方法。开发万年历界面时,可以采用Swing或...

    commons-lang3-3.1.jar

    - `FastDateFormat` 是一个快速且线程安全的日期格式化器。 4. **枚举处理**: - `EnumUtils` 提供了对Java枚举类型的实用方法,如获取枚举值、检查枚举是否存在等。 5. **数学运算**: - `MathUtils` 提供了...

    commons-lang-1.0.tar.gz

    5. **日期/时间**:`DateUtils`和`FastDateFormat`类提供了日期和时间的处理功能,可以方便地进行日期格式化和解析。 6. **枚举支持**:在Java 5之前,没有内置的枚举类型,`EnumUtils`提供了对枚举的模拟和支持。 ...

    commons-lang-2.4.jar.zip

    4. **日期时间处理**:SAP的日期时间格式可能与Java的标准格式不同,`DateUtils`和`FastDateFormat`类可以方便地进行日期时间的格式化和解析,确保数据的一致性。 5. **反射工具**:在创建和操作SAP接口对象时,`...

    Java 计算农历全_java获取农历_java_日历_greens1r_农历_

    首先,Java标准库并没有内置对农历的支持,但我们可以使用第三方库如"ChineseCalendar"、"Joda-Time"(配合"ChineseLunisolarCalendar"扩展)或者"FastDateFormat"等来实现这一功能。这些库通常提供了转换公历和农历...

    java工具类

    此外,Apache Commons Lang的DateUtils和FastDateFormat也是常用的日期时间处理工具。 3. **Number工具类**:处理数字时,工具类可以帮助进行格式化、转换和数学运算。例如,BigDecimal和BigInteger用于高精度计算...

    JAVA万年历源代码

    在Java中,实现农历转换通常需要自定义算法或引用第三方库,如`FastDateFormat`或`org.apache.commons.lang3.time`包中的类用于日期格式化,而`com.github.zengfr.conunian`或`com.lunarcalendar.io.LunarCalendar`...

    commons-lang-3-3.1 jar包和源码包

    4. **日期与时间**:DateUtils 提供了日期和时间的处理,如格式化、解析、比较等,而FastDateFormat 是一个高性能的日期格式化工具,比Java内置的SimpleDateFormat更快。 5. **枚举操作**:EnumUtils 提供了处理...

    aduna-commons-lang-2.2.jar.zip

    DateUtils和FastDateFormat等类提供了高效的日期和时间格式化以及解析功能,避免了对Java自带的日期类的繁琐操作。 3. **数学计算**:MathUtils类包含了一些高级的数学函数,如幂运算、开方、随机数生成等,为...

Global site tag (gtag.js) - Google Analytics