gpt4 book ai didi

Java Period.between 显示奇怪的输出

转载 作者:行者123 更新时间:2023-12-03 22:56:42 28 4
gpt4 key购买 nike

我正在制作一个仅用于学习目的的应用程序。我试图计算两个日期之间的差异

LocalDate today  = LocalDate.now();
LocalDate localHoudbaarheid = houdbaarheidsDatum;
Period period = Period.between(today, localHoudbaarheid);
计算有效,但输出很奇怪。
代码的输出:
Dagen tot over de datum: P9D
唯一应该显示的是 9没有 PD .这是我在代码中调用名称句点以将其打印出来的地方:
"Dagen tot over de datum: " + period;

最佳答案

输出没有什么奇怪的,正是文档 ,即 Period.toString() 的 javadoc说它是:

Outputs this period as a String, such as P6Y3M1D.

The output will be in the ISO-8601 period format. A zero period will be represented as zero days, 'P0D'.


正如文档所说,输出遵循 ISO-8601 standard持续时间,如 Wikipedia 上所述:

Durations define the amount of intervening time in a time interval and are represented by the format P[n]Y[n]M[n]DT[n]H[n]M[n]S ...


  • P is the duration designator (for period) placed at the start of the duration representation.

    • Y is the year designator that follows the value for the number of years.
    • M is the month designator that follows the value for the number of months.
    • W is the week designator that follows the value for the number of weeks.
    • D is the day designator that follows the value for the number of days.

关于Java Period.between 显示奇怪的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64756462/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com