如何在Joda中使用JDK GregorianCalendar对象日期 [英] How to use JDK GregorianCalendar object dates with Joda

查看:73
本文介绍了如何在Joda中使用JDK GregorianCalendar对象日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Joda库,因为Java本机方法的计数周期令人头疼,而且我的所有尝试都给出了不准确的结果

I'm trying to use Joda library since count periods with Java native methods is a pain in the neck and all my attempts give unprecise results

我看过这个样本

int n = Days.daysBetween(start.toLocalDate(), end.toLocalDate()).getDays();

由于我所有的班级都管理GregorianCalendar,因此我需要一种计算支持GregorianCalendar的天数的方法,例如

since all my classes manage GregorianCalendar, I need that method that counts the days support GregorianCalendar, something like

 public int countDays(GregorianCalendar start, GregorianCalendar end){
     //convert to joda start and end
     ...
     return Days.daysBetween(start.toLocalDate(), end.toLocalDate()).getDays();
 }

所以我的问题是: 如何将GregorianCalendar对象转换和重新转换为Joda管理的对象而没有副作用?

So my question: How to convert and reconvert GregorianCalendar object to the object managed by Joda without side effects?

推荐答案

使用带有ObjectDateTime构造函数,该构造函数可以"GregorianCalendar

Use the DateTime constructor that takes an Object, which can "include ReadableInstant, String, Calendar and Date." It specifically mentions GregorianCalendar, as well.

public int countDays(GregorianCalendar gregStart, GregorianCalendar gregEnd) {
    DateTime start = new DateTime(gregStart);
    DateTime end = new DateTime(gregEnd);
    return Days.daysBetween(start, end).getDays();
}

这篇关于如何在Joda中使用JDK GregorianCalendar对象日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆