Joda-time Example

学习日志 专栏收录该内容
36 篇文章 0 订阅
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10</version>
        </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,LocalTime,LocalDateTime : 表示时间的一部分,不可变,线程安全
        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());
  • Interval: 表示两个 Instant 之间的毫秒值,包含了完整的时间定义
        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();
  • Duration: 用毫秒值表示的日期区间
        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));
  • Period: 似乎是用在计算两个日期相隔多少个天(或周/年/月/时/分/秒/毫秒)
        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()));

完整代码示例

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

onion0715

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值