日期的处理在我们使用java进行开发的时候是一个十分繁复的部分,尤其是在有大量分析功能的系统中(比如我们公司的系统),时间的处理更是让人头疼。
所以现在让我们来一起学习一下一个非常有名的Java时间封装工具——Joda-time。
首先我们先来对他有个大概的了解,这里我是用官方的示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
private boolean isAfterPayDay(DateTime datetime) { if (datetime.getMonthOfYear() == 2) { // February is month 2!! return datetime.getDayOfMonth() > 26; } return datetime.getDayOfMonth() > 28; } private Days daysToNewYear(LocalDate fromDate) { LocalDate newYear = fromDate.plusYears(1).withDayOfYear(1); return Days.daysBetween(fromDate, newYear); } private boolean isRentalOverdue(DateTime datetimeRented) { Period rentalPeriod = new Period().withDays(2).withHours(12); return datetimeRented.plus(rentalPeriod).isBeforeNow(); } private String getBirthMonthText(LocalDate dateOfBirth) { return dateOfBirth.monthOfYear().getAsText(Locale.ENGLISH); } |
我这里也准备了示例代码,见github的ch01:
https://github.com/irfen/joda-time-example
我在这里先不做过多介绍,大家可以自行看一下代码大概猜一下具体的意思是什么,我们之后会详细介绍这些用法。
本文原创于赵伊凡BLOG
©原创文章,转载请注明来源: 赵伊凡's Blog
©本文链接地址: 1、Joda-time入门教程(一)
“1、Joda-time入门教程(一)”的32个回复