Joda-time学习笔记

来源:互联网 发布:澳大利亚土著居民知乎 编辑:程序博客网 时间:2024/05/21 21:49
Joda-time学习笔记

一、joda的优点展示

joda-time能够便捷地格式化时间输出、设定时间、加减时间、计算时间差值。跟JDK的Date/Calender相比一试便知,每个测试中上半部分是用jdk操作,下半部是用joda-time操作。最后一个我想……实在不想用jdk来实现:

public class JodaTimeTest {    @Test    public void testPrintDate(){        Date date = new Date();        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");        String dateText = format.format(date);        Assert.assertEquals("2014-11-13", dateText);        DateTime dateTime = new DateTime();        dateText = dateTime.toString("yyyy-MM-dd");        Assert.assertEquals("2014-11-13", dateText);    }    @Test    public void testAddDate(){        Calendar calendar = Calendar.getInstance();        calendar.set(2008, Calendar.AUGUST, 8000);        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E HH:mm:ss");        calendar.add(Calendar.DAY_OF_MONTH, 90);        String dateText = sdf.format(calendar.getTime());        Assert.assertEquals("2008年11月06日 星期四 00:00:00", dateText);        DateTime dateTime = new DateTime(2008,880000);        dateText = dateTime.plusDays(90).toString("yyyy年MM月dd日 E HH:mm:ss");        Assert.assertEquals("2008年11月06日 星期四 00:00:00", dateText);    }    @Test    public void testCalcDate(){        Calendar calendar = Calendar.getInstance();        calendar.set(2012, Calendar.DECEMBER, 20000);        calendar.add(Calendar.YEAR, 9);        calendar.add(Calendar.MONTH, 5);        calendar.add(Calendar.WEEK_OF_MONTH, 2);        calendar.add(Calendar.DAY_OF_MONTH, 7);        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 E");        String dateText = df.format(calendar.getTime());        Assert.assertEquals("2022年06月10日 星期五", dateText);        LocalDate birthDate = new LocalDate(20121220);        dateText = birthDate.plusYears(9).plusMonths(5).plusWeeks(2).plusDays(7).toString("yyyy年MM月dd日 E");        Assert.assertEquals("2022年06月10日 星期五", dateText);    }    @Test    public void testSomeDate(){;        LocalDate date = new LocalDate(19901024);        int days = Days.daysBetween(date, new LocalDate()).getDays();        Assert.assertEquals(8786, days);    }}

这三个是比较常见的操作,另外还可以用来计算本周是第几周、某天是星期几(我在生产环境中已使用的接口)等,其他api大家可以参考官网。

二、joda与long、Date、Calendar互转

与三者的互转十分简便,所以joda-time完全可以取代jdk中日期来运算,只要就结果再转换就行。

public class JodaTurnTest {    @Test    public void testJoda2Long(){        long time =  1415861538986L;        DateTime date = new DateTime(time);        Assert.assertEquals(time, date.getMillis());  //将时间粒度锁定在秒级别    }    @Test    public void  testJoda2Date(){        Date dateSource = new Date();        DateTime dateTime = new DateTime(dateSource);        Assert.assertEquals(dateSource.getTime(), dateTime.getMillis());        Date date2 = dateTime.toDate();        Assert.assertEquals(dateSource, date2);    }    @Test    public void testJoda2Calender(){        Calendar cal = Calendar.getInstance();        DateTime dateTime = new DateTime(cal);        Assert.assertEquals(cal.getTimeInMillis(), dateTime.getMillis());        Calendar calendar = dateTime.toGregorianCalendar();        Assert.assertEquals(cal, calendar);    }}

三 、joda的概念


Instant:连续时间轴上的某个瞬间,即某时刻,采用UTC 1970年1月1日 00:00:00到目前时刻经历的毫秒数。与unix和jdk中的相同。 joda-time中主类Datetime就采用了Instant,这样就可以与JDK中date,calender交互了。

  joda1 


Partial:日常生活中的时间点,只是一个时间片段,如11点20分,再如9月20号。 Joda-timeAPI中LocalDate、LocalTime、LocalDateTime、YearMonth、MonthDay、Partial、YearMonthDay、TimeOfDay类都是这种概念。joda2 


Interval:表达的是两个时刻之间的区间段。如: 

joda3

DateTime start = new DateTime(2004110000);DateTime end = new DateTime(2005110000);Interval interval = new Interval(start, end);


在Interval类接口中,可以得到开始、结束、是否包含等等

DateTime start = interval.getStart();DateTime end = interval.getEnd();DateTime testDate = new DateTime(2004210000);boolean contains = interval.contains(testDate);


Duration:表示的目前的时刻再持续多久时间,与之前的Interval时间区间该概念类似,不过单位是毫秒。 joda4

DateTime start = new DateTime(1975526000);Duration oneThousandMillis = new Duration(1000);DateTime end = start.plus(oneThousandMillis);


Period:与uration概念类似,不过单位不是毫秒,而是更人性化的单位,如年、月、日。这类的包括Period、MutablePeriod、Years、Months、Weeks、Days Hours、Minutes、Seconds等类。 

joda5

DateTime start = new DateTime(1975526000);DateTime end = new DateTime(1978723000);Days days = Days.daysBetween(start, end);

四、joda与java8与date4j

java8中提供了新的日期API,而提供者正是Joda。可查看JSR310( https://jcp.org/en/jsr/detail?id=310)。 

date4j是针对joda庞大类系与年表体系,而提供一套极简Api, 可查看( http://www.date4j.net/)。比较遗憾暂时还没有发现中国农历操作的api。

五、参考地址:

ibm上的简介: http://www.ibm.com/developerworks/cn/java/j-jodatime.html 

台湾一哥们写的,本文图片都是来着他的bolg: http://www.codedata.com.tw/java/jodatime-jsr310-3-using-jodatime/ 

官网: http://www.joda.org/joda-time/

1 0