`
sunxboy
  • 浏览: 2886111 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Joda-time 使用

 
阅读更多

JDK Interoperability

The DateTime class has a constructor which takes an Object as input. In particular this constructor can be passed a JDK Date, JDK Calendar or JDK GregorianCalendar (It also accepts an ISO8601 formatted String, or Long object representing milliseconds). This is one half of the interoperability with the JDK. The other half of interoperability with JDK is provided by DateTime methods which return JDK objects.

Thus inter-conversion between Joda DateTime and JDK Date can be performed as follows

    // from Joda to JDK
    DateTime dt = new DateTime();
    Date jdkDate = dt.toDate();

    // from JDK to Joda
    dt = new DateTime(jdkDate);

Similarly, for JDK Calendar:

    // from Joda to JDK
    DateTime dt = new DateTime();
    Calendar jdkCal = dt.toCalendar(Locale.CHINESE);

    // from JDK to Joda
    dt = new DateTime(jdkCal);

and JDK GregorianCalendar:

    // from Joda to JDK
    DateTime dt = new DateTime();
    GregorianCalendar jdkGCal = dt.toGregorianCalendar();

    // from JDK to Joda
    dt = new DateTime(jdkGCal);

 

 

 

Changing TimeZone

DateTime comes with support for a couple of common timezone calculations. For instance, if you want to get the local time in London at this very moment, you would do the following

    // get current moment in default time zone
    DateTime dt = new DateTime();
    // translate to London local time
    DateTime dtLondon = dt.withZone(DateTimeZone.forID("Europe/London"));
where DateTimeZone.forID("Europe/London") returns the timezone value for London. The resulting value dtLondon has the same absolute millisecond time, but a different set of field values.

There is also support for the reverse operation, i.e. to get the datetime (absolute millisecond) corresponding to the moment when London has the same local time as exists in the default time zone now. This is done as follows

    // get current moment in default time zone
    DateTime dt = new DateTime();
    // find the moment when London will have / had the same time
    dtLondonSameTime = dt.withZoneRetainFields(DateTimeZone.forID("Europe/London"));

A set of all TimeZone ID strings (such as "Europe/London") may be obtained by calling DateTimeZone.getAvailableIDs(). A full list of available time zones is provided here.

 

 

分享到:
评论

相关推荐

    joda-time-2.8.1-API文档-中文版.zip

    赠送jar包:joda-time-2.8.1.jar 赠送原API文档:joda-time-2.8.1-javadoc.jar 赠送源代码:joda-time-2.8.1-sources.jar 包含翻译后的API文档:joda-time-2.8.1-javadoc-API文档-中文(简体)版.zip 对应Maven信息...

    joda-time-2.5.zip

    本文将深入探讨Joda-Time 2.5版本的核心特性和使用方法。 一、Joda-Time简介 Joda-Time是一款开源的Java日期和时间处理库,由Stephen Colebourne创建,旨在替换Java SE 1.4及之前的日期时间API。它提供了一个更...

    joda-time-2.10.8-API文档-中文版.zip

    赠送jar包:joda-time-2.10.8.jar; 赠送原API文档:joda-time-2.10.8-javadoc.jar; 赠送源代码:joda-time-2.10.8-sources.jar; 赠送Maven依赖信息文件:joda-time-2.10.8.pom; 包含翻译后的API文档:joda-time-...

    joda-time-2.1.jar

    joda-time-2.1.jar

    joda-time-2.9.5-API文档-中文版.zip

    赠送jar包:joda-time-2.9.5.jar; 赠送原API文档:joda-time-2.9.5-javadoc.jar; 赠送源代码:joda-time-2.9.5-sources.jar; 赠送Maven依赖信息文件:joda-time-2.9.5.pom; 包含翻译后的API文档:joda-time-...

    joda-time-2.5-API文档-中文版.zip

    赠送jar包:joda-time-2.5.jar; 赠送原API文档:joda-time-2.5-javadoc.jar; 赠送源代码:joda-time-2.5-sources.jar; 赠送Maven依赖信息文件:joda-time-2.5.pom; 包含翻译后的API文档:joda-time-2.5-javadoc-...

    joda-time-2.2-API文档-中英对照版.zip

    赠送jar包:joda-time-2.2.jar; 赠送原API文档:joda-time-2.2-javadoc.jar; 赠送源代码:joda-time-2.2-sources.jar; 赠送Maven依赖信息文件:joda-time-2.2.pom; 包含翻译后的API文档:joda-time-2.2-javadoc-...

    joda-time-2.9.9-API文档-中英对照版.zip

    赠送jar包:joda-time-2.9.9.jar; 赠送原API文档:joda-time-2.9.9-javadoc.jar; 赠送源代码:joda-time-2.9.9-sources.jar; 赠送Maven依赖信息文件:joda-time-2.9.9.pom; 包含翻译后的API文档:joda-time-...

    joda-time-2.10.4-API文档-中文版.zip

    赠送jar包:joda-time-2.10.4.jar; 赠送原API文档:joda-time-2.10.4-javadoc.jar; 赠送源代码:joda-time-2.10.4-sources.jar; 赠送Maven依赖信息文件:joda-time-2.10.4.pom; 包含翻译后的API文档:joda-time-...

    joda-time-android,具有android专门化的joda时间库.zip

    在完成上述步骤后,即可在项目中自由地使用`joda-time-android`提供的功能,提升日期时间处理的效率和便利性。 总之,`joda-time-android`是一个专门为Android开发设计的日期时间处理库,它结合了Joda-Time的强大...

    joda-time2.4源码及编译

    1. **格式化与解析**:Joda-Time提供DateTimeFormatter,可以自定义日期时间格式进行解析和格式化,避免了使用SimpleDateFormat带来的线程安全问题。 2. **时间区间操作**:通过Interval类,可以方便地进行时间区间...

    joda-time-2.3

    1. 易于使用:Calendar让获取"正常的"的日期变得很困难,使它没办法提供简单的方法,而Joda-Time能够 直接进行访问域并且索引值1就是代表January。 2. 易于扩展:JDK支持多日历系统是通过Calendar的子类来实现,这样...

    joda-time-2.10.6.jar

    Joda-Time 令时间和日期值变得易于管理、操作和理解。日期和时间是一个非常复杂的领域。Joda-Time中的许多类旨在使域的细微差别得以充分表达。版本2.10.6是当前的最新版本。该版本被认为是稳定的,值得2.x标签使用。

    joda-time-2.9.9

    在解压后的文件中,`joda-time-2.9.9.jar`是Joda-Time库的二进制文件,可以直接引入到Java项目中使用。而`使用说明.txt`文件则包含了详细的使用指南和示例,对于初学者来说,是快速掌握Joda-Time API的重要参考资料...

    joda-time-2.10.13-API文档-中英对照版.zip

    赠送jar包:joda-time-2.10.13.jar; 赠送原API文档:joda-time-2.10.13-javadoc.jar; 赠送源代码:joda-time-2.10.13-sources.jar; 赠送Maven依赖信息文件:joda-time-2.10.13.pom; 包含翻译后的API文档:joda-...

    joda-time-2.9.9-dist.tar.gz

    早期的Java标准库中的日期时间API(java.util.Date和Calendar)设计复杂,不易使用,而Joda-Time库的出现正是为了解决这一问题。本文将详细介绍Joda-Time库,以及其在2.9.9版本中的特点和用法。 Joda-Time是Java...

    joda-time源码

    在本文中,我们将深入探讨Joda-Time的核心概念、主要功能以及如何在实际项目中使用它。 1. **核心概念** Joda-Time基于“瞬时值”(Instant)和“时期”(Period)的概念。"瞬时值"表示自纪元以来的毫秒数,而...

    joda-time-1.6.2.jar

    本文将详细讨论这个问题以及如何使用"joda-time-1.6.2.jar"来解决它。 首先,我们需要了解为什么Spring MVC在尝试将日期字段转换为`Date`类型时会报错。这主要是因为默认的转换器并不支持用户友好的日期格式,如...

    joda-time-2.7-dist

    《Joda-Time:Java平台上的高效日期时间处理库》 Joda-Time,作为一个知名的开源库,专注于为Java平台提供高效且易用的时间日期处理功能。版本2.7是该库的一个重要里程碑,它包含了源代码和Java文档,使得开发者...

Global site tag (gtag.js) - Google Analytics