Java 字符串的句点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1440557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:18:04  来源:igfitidea点击:

Period to string

javajodatimeperiod

提问by

I'm using the Joda-Timelibrary 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".

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

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 totalamount of seconds with getSeconds.

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

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

如何让 Joda 计算各个字段的秒数,例如天数、小时数等......?

采纳答案by SteveD

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)

由 ripper234 编辑- 添加TL;DR 版本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).

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

回答by Jherico

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

回答by simao

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

回答by marucf

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.

你错过了时间,这就是为什么。几天后追加数小时,问题就解决了。