jodatime java8交互_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, 8, 0, 0, 0);

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,8, 8, 0, 0, 0, 0);

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, 20, 0, 0, 0);

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(2012, 12, 20);

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(1990, 10, 24);

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交互了。

4ef3f4b5a741ea6811a78c4d8a4fe5c8.png

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

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

573905463c4b57490c5271b647b19b1c.png

DateTime start = new DateTime(2004, 1, 1, 0, 0, 0, 0);

DateTime end = new DateTime(2005, 1, 1, 0, 0, 0, 0);

Interval interval = new Interval(start, end);

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

DateTime start = interval.getStart();

DateTime end = interval.getEnd();

DateTime testDate = new DateTime(2004, 2, 1, 0, 0, 0, 0);

boolean contains = interval.contains(testDate);

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

DateTime start = new DateTime(1975, 5, 26, 0, 0, 0);

Duration oneThousandMillis = new Duration(1000);

DateTime end = start.plus(oneThousandMillis);

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

dc9b87dfae68d386f8d47becd914306b.png

DateTime start = new DateTime(1975, 5, 26, 0, 0, 0);

DateTime end = new DateTime(1978, 7, 23, 0, 0, 0);

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。

五、参考地址:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值