JodaTime初体验

前段时间用JDK自带的Calendar类来处理日期,需要获取年、季,月,星期的起始日期,被折腾得要死要活。看了这篇文章 http://www.blogbus.com/dreamhead-logs/226738702.html 后,决定使用JodaTime重新写一下这个功能。

关于JodaTime可以看这篇文章:http://www.ibm.com/developerworks/cn/java/j-jodatime.html

废话不多说,直接贴代码。

 import java.util.Date;

 import org.joda.time.DateTime;

 public class JodaDemo {
public static final int PERIOD_TYPE_YEAR = 0; public static final int PERIOD_TYPE_MONTH = 1; public static final int PERIOD_TYPE_HALFMONTH = 2; public static final int PERIOD_TYPE_WEEK = 3; public static final int PERIOD_TYPE_BIWEEK = 4; public static final int PERIOD_TYPE_QUARTER = 5; /**
* 根据type获取日期期间 type为月的话 则返回月初与月末两个Date
*
* @param currentDate
* @param type
* @return
*/
public static Date[] getPeriodByType(Date currentDate, int type) { DateTime date = new DateTime(currentDate);
Date fromDate = currentDate;
Date toDate = currentDate; switch (type) {
case PERIOD_TYPE_YEAR:
fromDate = date.dayOfYear().withMinimumValue().toDate();
toDate = date.dayOfYear().withMaximumValue().toDate();
break; case PERIOD_TYPE_QUARTER:
int month = date.getMonthOfYear();
month = (month - 1) % 3;
date = date.plusMonths(-month);
fromDate = date.dayOfMonth().withMinimumValue().toDate();
date = date.plusMonths(2);
toDate = date.dayOfMonth().withMaximumValue().toDate();
break; case PERIOD_TYPE_MONTH:
fromDate = date.dayOfMonth().withMinimumValue().toDate();
toDate = date.dayOfMonth().withMaximumValue().toDate();
break; case PERIOD_TYPE_WEEK:
fromDate = date.dayOfWeek().withMinimumValue().toDate();
toDate = date.dayOfWeek().withMaximumValue().toDate(); break; case PERIOD_TYPE_BIWEEK:
fromDate = date.dayOfYear().withMinimumValue().toDate();
toDate = date.dayOfYear().withMaximumValue().toDate();
break; case PERIOD_TYPE_HALFMONTH: fromDate = date.dayOfYear().withMinimumValue().toDate();
toDate = date.dayOfYear().withMaximumValue().toDate(); break; default:
break;
} Date[] period = new Date[2];
period[0] = fromDate;
period[1] = toDate;
return period;
}
}

然后写测试:

 import static org.junit.Assert.*;

 import java.util.Date;

 import org.joda.time.DateTime;
import org.junit.Test; public class JodaDemoTest {
public static String dateFormat = "yyyy-MM-dd";
@Test
public void testGetPeriodByType() {
DateTime dateTime = new DateTime("2013-12-26");
Date[] weeks = JodaDemo.getPeriodByType(dateTime.toDate(), JodaDemo.PERIOD_TYPE_WEEK);
assertEquals("2013-12-23", new DateTime(weeks[0]).toString(dateFormat));
assertEquals("2013-12-29", new DateTime(weeks[1]).toString(dateFormat)); // Date[] biWeek = JodaDemo.getPeriodByType(dateTime.toDate(), JodaDemo.PERIOD_TYPE_BIWEEK);
// assertEquals("2013-12-01", new DateTime(biWeek[0]).toString(dateFormat));
// assertEquals("2013-12-31", new DateTime(biWeek[1]).toString(dateFormat)); Date[] months = JodaDemo.getPeriodByType(dateTime.toDate(), JodaDemo.PERIOD_TYPE_MONTH);
assertEquals("2013-12-01", new DateTime(months[0]).toString(dateFormat));
assertEquals("2013-12-31", new DateTime(months[1]).toString(dateFormat)); Date[] quarters = JodaDemo.getPeriodByType(dateTime.toDate(), JodaDemo.PERIOD_TYPE_QUARTER);
assertEquals("2013-10-01", new DateTime(quarters[0]).toString(dateFormat));
assertEquals("2013-12-31", new DateTime(quarters[1]).toString(dateFormat)); Date[] years = JodaDemo.getPeriodByType(dateTime.toDate(), JodaDemo.PERIOD_TYPE_YEAR);
assertEquals("2013-01-01", new DateTime(years[0]).toString(dateFormat));
assertEquals("2013-12-31", new DateTime(years[1]).toString(dateFormat));
} }

测试通过:

JodaTime初体验

注:按两周BIWEEK分割和按半个月HALFMONTH分割的功能还没完善。

使用Joda果然简单了很多,接下来有空可以看看源码,再深入理解一下。

上一篇:ABP架构学习系列一 整体项目结构及目录


下一篇:Java中Timer和TimerTask的使用