JodaTime初体验
前段时间用JDK自带的Calendar类来处理日期,需要获取年、季,月,星期的起始日期,被折腾得要死要活。看了这篇文章 http://www.blogbus.com/dreamhead-logs/226738702.html 后,决定使用JodaTime重新写一下这个功能。
关于JodaTime可以看这篇文章:http://www.ibm.com/developerworks/cn/java/j-jodatime.html
废话不多说,直接贴代码。
1 import java.util.Date; 2 3 import org.joda.time.DateTime; 4 5 public class JodaDemo { 6 public static final int PERIOD_TYPE_YEAR = 0; 7 8 public static final int PERIOD_TYPE_MONTH = 1; 9 10 public static final int PERIOD_TYPE_HALFMONTH = 2; 11 12 public static final int PERIOD_TYPE_WEEK = 3; 13 14 public static final int PERIOD_TYPE_BIWEEK = 4; 15 16 public static final int PERIOD_TYPE_QUARTER = 5; 17 18 /** 19 * 根据type获取日期期间 type为月的话 则返回月初与月末两个Date 20 * 21 * @param currentDate 22 * @param type 23 * @return 24 */ 25 public static Date[] getPeriodByType(Date currentDate, int type) { 26 27 DateTime date = new DateTime(currentDate); 28 Date fromDate = currentDate; 29 Date toDate = currentDate; 30 31 switch (type) { 32 case PERIOD_TYPE_YEAR: 33 fromDate = date.dayOfYear().withMinimumValue().toDate(); 34 toDate = date.dayOfYear().withMaximumValue().toDate(); 35 break; 36 37 case PERIOD_TYPE_QUARTER: 38 int month = date.getMonthOfYear(); 39 month = (month - 1) % 3; 40 date = date.plusMonths(-month); 41 fromDate = date.dayOfMonth().withMinimumValue().toDate(); 42 date = date.plusMonths(2); 43 toDate = date.dayOfMonth().withMaximumValue().toDate(); 44 break; 45 46 case PERIOD_TYPE_MONTH: 47 fromDate = date.dayOfMonth().withMinimumValue().toDate(); 48 toDate = date.dayOfMonth().withMaximumValue().toDate(); 49 break; 50 51 case PERIOD_TYPE_WEEK: 52 fromDate = date.dayOfWeek().withMinimumValue().toDate(); 53 toDate = date.dayOfWeek().withMaximumValue().toDate(); 54 55 break; 56 57 case PERIOD_TYPE_BIWEEK: 58 fromDate = date.dayOfYear().withMinimumValue().toDate(); 59 toDate = date.dayOfYear().withMaximumValue().toDate(); 60 break; 61 62 case PERIOD_TYPE_HALFMONTH: 63 64 fromDate = date.dayOfYear().withMinimumValue().toDate(); 65 toDate = date.dayOfYear().withMaximumValue().toDate(); 66 67 break; 68 69 default: 70 break; 71 } 72 73 Date[] period = new Date[2]; 74 period[0] = fromDate; 75 period[1] = toDate; 76 return period; 77 } 78 }
然后写测试:
1 import static org.junit.Assert.*; 2 3 import java.util.Date; 4 5 import org.joda.time.DateTime; 6 import org.junit.Test; 7 8 9 public class JodaDemoTest { 10 public static String dateFormat = "yyyy-MM-dd"; 11 @Test 12 public void testGetPeriodByType() { 13 DateTime dateTime = new DateTime("2013-12-26"); 14 Date[] weeks = JodaDemo.getPeriodByType(dateTime.toDate(), JodaDemo.PERIOD_TYPE_WEEK); 15 assertEquals("2013-12-23", new DateTime(weeks[0]).toString(dateFormat)); 16 assertEquals("2013-12-29", new DateTime(weeks[1]).toString(dateFormat)); 17 18 // Date[] biWeek = JodaDemo.getPeriodByType(dateTime.toDate(), JodaDemo.PERIOD_TYPE_BIWEEK); 19 // assertEquals("2013-12-01", new DateTime(biWeek[0]).toString(dateFormat)); 20 // assertEquals("2013-12-31", new DateTime(biWeek[1]).toString(dateFormat)); 21 22 Date[] months = JodaDemo.getPeriodByType(dateTime.toDate(), JodaDemo.PERIOD_TYPE_MONTH); 23 assertEquals("2013-12-01", new DateTime(months[0]).toString(dateFormat)); 24 assertEquals("2013-12-31", new DateTime(months[1]).toString(dateFormat)); 25 26 Date[] quarters = JodaDemo.getPeriodByType(dateTime.toDate(), JodaDemo.PERIOD_TYPE_QUARTER); 27 assertEquals("2013-10-01", new DateTime(quarters[0]).toString(dateFormat)); 28 assertEquals("2013-12-31", new DateTime(quarters[1]).toString(dateFormat)); 29 30 Date[] years = JodaDemo.getPeriodByType(dateTime.toDate(), JodaDemo.PERIOD_TYPE_YEAR); 31 assertEquals("2013-01-01", new DateTime(years[0]).toString(dateFormat)); 32 assertEquals("2013-12-31", new DateTime(years[1]).toString(dateFormat)); 33 } 34 35 }
测试通过:
注:按两周BIWEEK分割和按半个月HALFMONTH分割的功能还没完善。
使用Joda果然简单了很多,接下来有空可以看看源码,再深入理解一下。