使用 jodatime 查找剩余日期和时间

发布于 2024-12-14 02:33:30 字数 764 浏览 0 评论 0原文

我想使用 joda 时间进行比较(查找剩余天数和两天之间的时间)。 我正在使用两个像这样的 DateTime 对象(一个正在开始,另一个正在结束)

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

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

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

Period period = new Period();

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

现在我得到的结果是年,月,日, ETC... 在这种情况下,我总是会得到 1-30 之间的天数(而不是 31,45,49...(我想要的))。

那么我怎样才能得到这个东西(有没有我缺少的方法)或者我需要以编程方式处理这个问题,因为我读到 joda 时间非常灵活,所以我非常确定会有这样的方法。

如果您熟悉,请分享您的知识。

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();

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();

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Hello爱情风 2024-12-21 02:33:31

使用 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));

示例输出

startDateendDate 之间的周期为

47天12小时46分47秒


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

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

For example :

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));

Sample output

Period between startDate and endDate is

47 days 12 hours 46 minutes 47 seconds


Or

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

with output

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

蓝海 2024-12-21 02:33:31

如果您希望以更漂亮的格式输出,那么您也可以使用此代码片段

val startDate = DateTime.now()
val endDate = DateTime(endTime)
val period = Period(startDate, endDate)

val periodFormatter = PeriodFormatterBuilder().apply {
    when {
        period.days > 0 -> appendDays().appendSuffix(" day", " days")
        period.hours > 0 -> appendHours().appendSuffix(" hour", " hours")
        period.minutes > 0 -> appendMinutes().appendSuffix(" minute", " minutes")
    }
}.toFormatter()

periodFormatter.print(period)

If you want the output in prettier format then you can use this snippet too

val startDate = DateTime.now()
val endDate = DateTime(endTime)
val period = Period(startDate, endDate)

val periodFormatter = PeriodFormatterBuilder().apply {
    when {
        period.days > 0 -> appendDays().appendSuffix(" day", " days")
        period.hours > 0 -> appendHours().appendSuffix(" hour", " hours")
        period.minutes > 0 -> appendMinutes().appendSuffix(" minute", " minutes")
    }
}.toFormatter()

periodFormatter.print(period)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文