字符串句点

发布于 2024-08-04 14:43:25 字数 297 浏览 5 评论 0原文

我正在使用 Java 的 Joda-Time 库。我在尝试将 period 对象转换为“x 天,x 小时,x 分钟”格式的字符串时遇到一些困难。

这些Period对象首先通过添加一定的秒数来创建(它们被序列化为XML作为秒,然后从中重新创建)。如果我只是在其中使用 getHours() 等方法,我得到的只是零和 getSeconds 的总秒数。

我怎样才能让 Joda 将秒计算到相应的字段中,比如天、小时等......?

I'm using the Joda-Time library with Java. I'm having some difficulty trying to turn a Period object to a string in the format of "x days, x hours, x minutes".

These Period objects are first created by adding an amount of seconds to them (they are serialized to XML as seconds and then recreated from them). If I simply use the getHours() etc. methods in them, all I get is zero and the total amount of seconds with getSeconds.

How can I make Joda calculate the seconds into the respective fields, like days, hours, etc...?

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

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

发布评论

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

评论(4

楠木可依 2024-08-11 14:43:25

您需要标准化周期,因为如果您使用总秒数构建它,那么这就是它具有的唯一值。对其进行标准化会将其分解为总天数、分钟数、秒数等。

由 ripper234 编辑 - 添加 TL;DR 版本PeriodFormat.getDefault().print(period)

例如:

public static void main(String[] args) {
  PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
    .appendDays()
    .appendSuffix(" day", " days")
    .appendSeparator(" and ")
    .appendMinutes()
    .appendSuffix(" minute", " minutes")
    .appendSeparator(" and ")
    .appendSeconds()
    .appendSuffix(" second", " seconds")
    .toFormatter();

  Period period = new Period(72, 24, 12, 0);

  System.out.println(daysHoursMinutes.print(period));
  System.out.println(daysHoursMinutes.print(period.normalizedStandard()));
}

将打印:

24 minutes and 12 seconds
3 days and 24 minutes and 12 seconds

因此您可以看到非标准化周期的输出只是忽略了数字小时(它没有将 72 小时转换为 3 天)。

You need to normalize the period because if you construct it with the total number of seconds, then that's the only value it has. Normalizing it will break it down into the total number of days, minutes, seconds, etc.

Edit by ripper234 - Adding a TL;DR version: PeriodFormat.getDefault().print(period)

For example:

public static void main(String[] args) {
  PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
    .appendDays()
    .appendSuffix(" day", " days")
    .appendSeparator(" and ")
    .appendMinutes()
    .appendSuffix(" minute", " minutes")
    .appendSeparator(" and ")
    .appendSeconds()
    .appendSuffix(" second", " seconds")
    .toFormatter();

  Period period = new Period(72, 24, 12, 0);

  System.out.println(daysHoursMinutes.print(period));
  System.out.println(daysHoursMinutes.print(period.normalizedStandard()));
}

Will print:

24 minutes and 12 seconds
3 days and 24 minutes and 12 seconds

So you can see the output for the non-normalized period simply ignores the number of hours (it didn't convert the 72 hours to 3 days).

世界如花海般美丽 2024-08-11 14:43:25

您还可以使用默认格式化程序,这适用于大多数情况:

Period period = new Period(startDate, endDate);
System.out.println(PeriodFormat.getDefault().print(period))

You can also use the Default formatter, which is good for most cases:

Period period = new Period(startDate, endDate);
System.out.println(PeriodFormat.getDefault().print(period))
叫思念不要吵 2024-08-11 14:43:25
    Period period = new Period();
    // prints 00:00:00
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
    period = period.plusSeconds(60 * 60 * 12);
    // prints 00:00:43200
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
    period = period.normalizedStandard();
    // prints 12:00:00
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
    Period period = new Period();
    // prints 00:00:00
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
    period = period.plusSeconds(60 * 60 * 12);
    // prints 00:00:43200
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
    period = period.normalizedStandard();
    // prints 12:00:00
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
甲如呢乙后呢 2024-08-11 14:43:25
PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
    .appendDays()
    **.appendSuffix(" day", " days")
    .appendSeparator(" and ")
    .appendMinutes()
    .appendSuffix(" minute", " minutes")**
    .appendSeparator(" and ")
    .appendSeconds()
    .appendSuffix(" second", " seconds")
    .toFormatter();

你错过了时间,这就是原因。几天后追加几个小时,问题就解决了。

PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
    .appendDays()
    **.appendSuffix(" day", " days")
    .appendSeparator(" and ")
    .appendMinutes()
    .appendSuffix(" minute", " minutes")**
    .appendSeparator(" and ")
    .appendSeconds()
    .appendSuffix(" second", " seconds")
    .toFormatter();

you're missing the hours, that's why. Append hours after days and problem solved.

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