您现在的位置是:主页 > news > 金华网站推广/燃灯seo
金华网站推广/燃灯seo
admin2025/6/18 10:49:38【news】
简介金华网站推广,燃灯seo,小城市做网站,广州建设职业Joda-time学习笔记一、joda的优点展示 joda-time能够便捷地格式化时间输出、设定时间、加减时间、计算时间差值。跟JDK的Date/Calender相比一试便知,每个测试中上半部分是用jdk操作,下半部是用joda-time操作。最后一个我想……实在不想用jdk来实现: publ…
一、joda的优点展示
joda-time能够便捷地格式化时间输出、设定时间、加减时间、计算时间差值。跟JDK的Date/Calender相比一试便知,每个测试中上半部分是用jdk操作,下半部是用joda-time操作。最后一个我想……实在不想用jdk来实现:
public class JodaTimeTest {@Testpublic 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);}@Testpublic 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);}@Testpublic 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);}@Testpublic 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 {@Testpublic void testJoda2Long(){long time = 1415861538986L;DateTime date = new DateTime(time);Assert.assertEquals(time, date.getMillis()); //将时间粒度锁定在秒级别}@Testpublic 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);}@Testpublic 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交互了。
Partial:日常生活中的时间点,只是一个时间片段,如11点20分,再如9月20号。 Joda-timeAPI中LocalDate、LocalTime、LocalDateTime、YearMonth、MonthDay、Partial、YearMonthDay、TimeOfDay类都是这种概念。
Interval:表达的是两个时刻之间的区间段。如:
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时间区间该概念类似,不过单位是毫秒。
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等类。
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。
五、参考地址:
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/