Java日期和时间处理类库:Joda-Time

openkk 12年前
     <p>Joda-Time提供了一组Java类包用于处理包括ISO8601标准在内的date和time。可以利用它把JDK Date和Calendar类完全替换掉,而且仍然能够提供很好的集成。</p>    <p>Joda-Time主要的特点包括:</p>    <p>1. 易于使用:Calendar让获取"正常的"的日期变得很困难,使它没办法提供简单的方法,而Joda-Time能够 直接进行访问域并且索引值1就是代表January。<br /> 2. 易于扩展:JDK支持多日历系统是通过Calendar的子类来实现,这样就显示的非常笨重而且事实 上要实现其它日历系统是很困难的。Joda-Time支持多日历系统是通过基于Chronology类的插件体系来实现。<br /> 3. 提供一组完整的功能:它打算提供 所有关系到date-time计算的功能.Joda-Time当前支持6种日历系统,而且在将来还会继续添加。有着比JDK Calendar更好的整体性能等等。</p>    <p>下面是一些代码示例:</p>    <pre>public boolean isAfterPayDay(<a href="/misc/goto?guid=4959500027230414745"><strong>DateTime</strong></a> datetime) {   if (datetime.getMonthOfYear() == 2) {   // February is month 2!!     return datetime.getDayOfMonth() > 26;   }   return datetime.getDayOfMonth() > 28; }  public <a href="/misc/goto?guid=4959500027341307754"><strong>Days</strong></a> daysToNewYear(<a href="/misc/goto?guid=4959500027445421204"><strong>LocalDate</strong></a> fromDate) {   <a href="/misc/goto?guid=4959500027445421204"><strong>LocalDate</strong></a> newYear = fromDate.plusYears(1).withDayOfYear(1);   return <a href="/misc/goto?guid=4959500027341307754"><strong>Days</strong></a>.daysBetween(fromDate, newYear); }  public boolean isRentalOverdue(<a href="/misc/goto?guid=4959500027230414745"><strong>DateTime</strong></a> datetimeRented) {   <a href="/misc/goto?guid=4959500027566361337"><strong>Period</strong></a> rentalPeriod = new <a href="/misc/goto?guid=4959500027566361337"><strong>Period</strong></a>().withDays(2).withHours(12);   return datetimeRented.plus(rentalPeriod).isBeforeNow(); }  public String getBirthMonthText(<a href="/misc/goto?guid=4959500027445421204"><strong>LocalDate</strong></a> dateOfBirth) {   return dateOfBirth.monthOfYear().getAsText(Locale.ENGLISH); }</pre>    <p><strong>项目主页:</strong><a href="http://www.open-open.com/lib/view/home/1325143595311" target="_blank">http://www.open-open.com/lib/view/home/1325143595311</a></p>