Joda-TimeJava 日期時(shí)間處理庫
JodaTime 提供了一組Java類包用于處理包括ISO8601標(biāo)準(zhǔn)在內(nèi)的date和time。可以利用它把JDK Date和Calendar類完全替換掉,而且仍然能夠提供很好的集成。
Joda-Time主要的特點(diǎn)包括:
1. 易于使用:Calendar讓獲取"正常的"的日期變得很困難,使它沒辦法提供簡單的方法,而Joda-Time能夠 直接進(jìn)行訪問域并且索引值1就是代表January。
2. 易于擴(kuò)展:JDK支持多日歷系統(tǒng)是通過Calendar的子類來實(shí)現(xiàn),這樣就顯示的非常笨重而且事實(shí) 上要實(shí)現(xiàn)其它日歷系統(tǒng)是很困難的。Joda-Time支持多日歷系統(tǒng)是通過基于Chronology類的插件體系來實(shí)現(xiàn)。
3. 提供一組完整的功能:它打算提供 所有關(guān)系到date-time計(jì)算的功能.Joda-Time當(dāng)前支持6種日歷系統(tǒng),而且在將來還會(huì)繼續(xù)添加。有著比JDK Calendar更好的整體性能等等。
下面是一些代碼示例:
public boolean isAfterPayDay( datetime) {
if (datetime.getMonthOfYear() == 2) { // February is month 2!!
return datetime.getDayOfMonth() > 26;
}
return datetime.getDayOfMonth() > 28;
}
public daysToNewYear( fromDate) {
newYear = fromDate.plusYears(1).withDayOfYear(1);
return .daysBetween(fromDate, newYear);
}
public boolean isRentalOverdue( datetimeRented) {
rentalPeriod = new ().withDays(2).withHours(12);
return datetimeRented.plus(rentalPeriod).isBeforeNow();
}
public String getBirthMonthText( dateOfBirth) {
return dateOfBirth.monthOfYear().getAsText(Locale.ENGLISH);
}評論
圖片
表情
