查找剩余的日期和时间使用jodatime [英] Find Remaining day and time using jodatime

查看:448
本文介绍了查找剩余的日期和时间使用jodatime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用乔达时间比较(确定两个天的剩余天数和时间)。 我以两个DateTime对象像这样(一个开始而另一个结束)

I want to compare(finding remaining days and time between two days) using joda time. I am taking two DateTime object like this(one is starting and another is ending)

DateTime endDate  = new DateTime(2011,12,25,0,0,0,0);   
DateTime strtDate = new DateTime();

现在我有兴趣寻找剩余的日期和时间这样的 天:49小时:5分钟:52秒:45 (不考虑年份和月份在这里..)

Now i am interested to find remaining date and time like this days:49 Hrs:5 Min:52 Sec:45(Not considering Year and month here..)

现在我继续像这样的周期类

Now I go ahead with period class like this

Period period = new Period();

PeriodFormatter formatter = new PeriodFormatterBuilder()
    .appendSeconds()
    .appendMinutes()
    .appendHours()
    .appendDays()
    .appendMonths()
    .appendYears()
    .printZeroNever()
    .toFormatter();

现在的结果,我所得到的年,月,日,等... 在这种情况下,我将获得的天1-​​30 (不31,45,49 ......(我想))始终。

Now in result what i get year,month,Day,etc... in this case i will get days between 1-30(not 31,45,49...(which I want)) always.

所以,我怎么能得到这个东西(有没有,我很想念任何方法),或者我需要以编程方式处理这个问题,因为我读的乔达时间很灵活,我非常肯定会有任何这样的方法

So how can i get this thing(is there any method that I am missing) or I need to handle this programmatically, as I read that joda time is very flexible so I am very sure that there will be any method like this.

如果您熟悉那么亲切分享你的知识。

If you are familiar then kindly share your knowledge.

推荐答案

定义从天向下兼容所有标准领域的 <一个href="http://joda-time.sourceforge.net/apidocs/org/joda/time/PeriodType.html#dayTime%28%29">PeriodType.dayTime().

Defines all standard fields from days downwards with PeriodType.dayTime().

例如

DateTime startDate = DateTime.now(); // now() : since Joda Time 2.0
DateTime endDate = new DateTime(2011, 12, 25, 0, 0);

Period period = new Period(startDate, endDate, PeriodType.dayTime());

PeriodFormatter formatter = new PeriodFormatterBuilder()
        .appendDays().appendSuffix(" day ", " days ")
        .appendHours().appendSuffix(" hour ", " hours ")
        .appendMinutes().appendSuffix(" minute ", " minutes ")
        .appendSeconds().appendSuffix(" second ", " seconds ")
        .toFormatter();

System.out.println(formatter.print(period));

示例输出

之间的时间的startDate 结束日期

47天12小时46分钟47秒

47 days 12 hours 46 minutes 47 seconds

PeriodFormatter formatter = new PeriodFormatterBuilder()
        .appendPrefix("Day:", " Days:").appendDays()
        .appendPrefix(" Hour:", " Hours:").appendHours()
        .appendPrefix(" Minute:", " Minutes:").appendMinutes()
        .appendPrefix(" Second:", " Seconds:").appendSeconds()
        .toFormatter();

与输出

天:47小时:12分:46秒:47

Days:47 Hours:12 Minutes:46 Seconds:47

这篇关于查找剩余的日期和时间使用jodatime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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