joda-time: 是java实现的一个日期时间库,JDK7之前推荐使用,JDK8之后建议使用java.util.time
包下的日期时间库
站点:
https://www.joda.org/joda-time/
https://github.com/JodaOrg/joda-time
Maven:
<dependency>
<groupId>joda-timegroupId>
<artifactId>joda-timeartifactId>
<version>2.10version>
dependency>
Instant: 表示一个时间戳值,定义为一个时间自 1970-01-01T00:00Z 的毫秒数,其含义和JDK原生的 Date,Calendar的毫秒时间戳是一致的.joda-time内部通过年表(chronology)将毫秒值转换成对应的日历值,年月日时分秒.
Chronology: 年表,也就是历法.joda-time支持8种历法系统.默认的是 ISO,也就是现在通常说的公历.可以搜索"历法"查阅相关资料
DateTime : 用于替代jdk的Calendar
构造方法
DateTime dt = new DateTime();
System.out.println(dt);
DateTime dt_1 = new DateTime("2004-12-13T21:39:45.618-08:00");
System.out.println(dt_1);
DateTime dt_2 = new DateTime(dt_1.toInstant());
System.out.println(dt_2);
DateTime dt_3 = new DateTime(new Date());
System.out.println(dt_3);
DateTime dt_4 = new DateTime(2004, 1, 1, 0, 0, 0, 0);
System.out.println(dt_4);
DateTime dt_5 = new DateTime(new Date().getTime());
System.out.println(dt_5);
DateTime dt_6 = new DateTime("2018-10-06T10:01:02");
System.out.println(dt_6);
DateTime dt_7 = new DateTime(Calendar.getInstance());
System.out.println(dt_7);
与JDK的日期类互相转换
// from Joda to JDK
DateTime dt = new DateTime();
System.out.println(dt);
Date jdkDate = dt.toDate();
System.out.println(jdkDate);
// from JDK to Joda
dt = new DateTime(jdkDate);
System.out.println(dt);
Calendar calendar = dt.toCalendar(Locale.getDefault());
System.out.println(calendar);
System.out.println(Locale.getDefault());
System.out.println(Locale.SIMPLIFIED_CHINESE);
日期字段查询
DateTime dt = new DateTime("2018-10-06T21:59:00.000");
assertEquals("公元", dt.era().getAsText());
assertEquals("20", dt.centuryOfEra().getAsText());
assertEquals(2018, dt.getYear());
assertEquals(10, dt.getMonthOfYear());
assertEquals(6, dt.getDayOfMonth());
assertEquals(21, dt.getHourOfDay());
assertEquals(59, dt.getMinuteOfHour());
assertEquals(0, dt.getSecondOfMinute());
DateTime newDt = dt.plusWeeks(1);
System.out.println(dt);
System.out.println(newDt);
日期字段值修改
DateTime dt = new DateTime("2018-10-06T21:59:00.000");
System.out.println(dt);
System.out.println(dt.year().setCopy(2017));
System.out.println(dt.monthOfYear().setCopy(11));
System.out.println(dt.plusDays(1));
System.out.println(dt.plusMonths(1));
System.out.println(dt.plusYears(1));
System.out.println(dt.withZone(DateTimeZone.forID("Europe/London")));
System.out.println(dt.withZoneRetainFields(DateTimeZone.forID("Europe/London")));
日期字段值访问
DateTime dt = new DateTime("2018-10-06T21:59:00.000");
assertEquals("星期六", dt.dayOfWeek().getAsShortText());
assertEquals("Saturday", dt.dayOfWeek().getAsText(Locale.US));
assertEquals("6", dt.dayOfMonth().getAsText());
String a = dt.toString();
String b = dt.toString("dd:MM:yy");
String c = dt.toString("EEE", Locale.FRENCH);
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd");
String d = dt.toString(fmt);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
LocalDate date = new LocalDate(2004, 12, 25);
LocalTime time = new LocalTime(12, 20);
assertEquals(2004,date.getYear());
assertEquals(12,date.getMonthOfYear());
assertEquals(12,time.getHourOfDay());
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);
DateTime s = interval.getStart();
DateTime e = interval.getEnd();
Chronology chrono = interval.getChronology();
Duration duration = interval.toDuration();
Period period = interval.toPeriod();
DateTime start = new DateTime(2005, 1, 1, 0, 0, 0, 0);
DateTime end = new DateTime(2005, 1, 1, 1, 0, 1, 0);
Duration duration = new Duration(start, end);
assertEquals(3601000,duration.getMillis());
assertEquals(0,duration.getStandardDays());
assertEquals(1,duration.toStandardHours().getHours());
assertEquals(60,duration.toStandardMinutes().getMinutes());
assertEquals(3601,duration.toStandardSeconds().getSeconds());
DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2005, 1, 1, 0, 0, 0, 0);
Duration dur = new Duration(start, end);
DateTime calc = start.plus(dur);
assertEquals(0,end.compareTo(calc));
DateTime start = new DateTime(2005, 1, 1, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 2, 2, 1, 1, 1, 1);
Interval interval = new Interval(start, end);
Period period = interval.toPeriod();
assertEquals(1, period.getYears());
assertEquals(1, period.getMonths());
assertEquals(1, period.getDays());
assertEquals(0, period.getWeeks());
assertEquals(1, period.getHours());
assertEquals(1, period.getMinutes());
assertEquals(1, period.getSeconds());
assertEquals(1, period.getMillis());
DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 3, 1, 0, 0, 0, 0);
// period of 1 year and 7 days
Period period = new Period(start, end);
// calc will equal end
DateTime calc = start.plus(period);
assertEquals(0, end.compareTo(calc));
assertEquals(431,Days.daysBetween(start, end).getDays());
assertEquals(14,Months.monthsBetween(start, end).getMonths());
assertEquals(61,Weeks.weeksBetween(start,end).getWeeks());
DateTime date = new DateTime(2004, 12, 25, 0, 2, 1, 111);
assertEquals("2004-12-25 00:02:01.111",date.toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS")));
assertEquals("2004年12月25日 星期六 上午12时02分01秒 CST",date.toString(DateTimeFormat.fullDateTime()));
assertEquals("2004年12月25日 上午12时02分01秒",date.toString(DateTimeFormat.longDateTime()));
assertEquals("2004年12月25日",date.toString(DateTimeFormat.longDate()));
assertEquals("2004-12-25 0:02:01",date.toString(DateTimeFormat.mediumDateTime()));
assertEquals("04-12-25 上午12:02",date.toString(DateTimeFormat.shortDateTime()));
Year year = Year.of(2018);
assertEquals(false, year.isLeap());
DateTime a = new DateTime("2018-10-06T21:59:00");
DateTime b = new DateTime("2019-11-07T23:00:01");
Interval interval = new Interval(a,b);
Period period = interval.toPeriod();
assertEquals("日期相隔1年1月1日1时1分1秒",String.format("日期相隔%d年%d月%d日%d时%d分%d秒",
period.getYears(),
period.getMonths(),
period.getDays(),
period.getHours(),
period.getMinutes(),
period.getSeconds()
));
assertEquals("日期相隔时间戳34304461000",String.format("日期相隔时间戳%d",
interval.toDurationMillis()));
完整代码示例