Java 如何计算一个时期的天数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30833582/
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-11 10:13:14  来源:igfitidea点击:

How to calculate the number of days in a period?

javajava-8

提问by M.K.

For the following Periodcalculation:

对于以下Period计算:

Period.between(LocalDate.of(2015, 8, 1), LocalDate.of(2015, 9, 2))

the result is:

结果是:

P1M1D

This is equivalent to 31 days + 1 day = 32 days.

这相当于 31 天 + 1 天 = 32 天。

For this Period:

为此Period

Period.between(LocalDate.of(2015, 8, 1), LocalDate.of(2015, 10, 2))

the result is:

结果是:

P2M1D

This is equivalent to: 31 days (in August) + 30 days (in September) + 1 (in October) = 62 days

这相当于:31 天(八月)+ 30 天(九月)+ 1(十月)= 62 天

Is there a method in the java.timepackage which will give the number of days in a Period? I can't find one. Not sure if I have overlooked anything or if it is just plain not there.

包中是否有一种方法java.time可以给出 a 中的天数Period?我找不到一个。不确定我是否忽略了任何东西,或者它是否只是简单的不存在。

采纳答案by Saket Mittal

From the documentation:

文档

To define an amount of time with date-based values (years, months, days), use the Periodclass. The Periodclass provides various get methods, such as getMonths, getDays, and getYears.To present the amount >of time measured in a single unit of time, such as days, you can use the ChronoUnit.betweenmethod.

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
                   " months, and " + p.getDays() +
                   " days old. (" + p2 + " days total)");

The code produces output similar to the following:

You are 53 years, 4 months, and 29 days old. (19508 days total)

要使用基于日期的值(年、月、日)定义时间量,请使用Period该类。的Period类提供各种获取方法,例如getMonthsgetDays,和getYears。要呈现在时间的单个单元,测量的时间量>如天,可以使用 ChronoUnit.between方法。

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
                   " months, and " + p.getDays() +
                   " days old. (" + p2 + " days total)");

该代码生成类似于以下内容的输出:

You are 53 years, 4 months, and 29 days old. (19508 days total)

回答by K Erlandsson

There is no way to do what you ask. The reason is that it is not possible from a Periodto deduce the actual number of calendar days in the period. A Periodis not tied to specific dates, once constructed in the way you show, it loses track of the actual calendar dates.

没有办法按照你的要求去做。原因是无法从 aPeriod推导出该期间的实际日历天数。APeriod与特定日期无关,一旦按照您显示的方式构建,它就会失去对实际日历日期的跟踪。

For example your first period represents a period of 1 month and 1 day. But the period does not care which month. It is simply a concept of "a month and a day".

例如,您的第一个时期代表 1 个月零 1 天的时期。但期间不在乎是哪个月。它只是一个“一个月零一天”的概念。

If you need the number of days between two dates you should use ChronoUnit.DAYS.betweenas Saket Mittal writes.

如果您需要两个日期之间的天数,您应该ChronoUnit.DAYS.between像 Saket Mittal 所写的那样使用。

回答by Ualter Jr.

There's a specific object depending at the amount of time you'd like to deal with. This page hereis very useful explaining which is best for your scenario.

有一个特定的对象取决于你想要处理的时间。此处的此页面非常有用,可解释哪个最适合您的场景。

The ChronoUnit.between method is useful when you want to measure an amount of time in a single unit of time only, such as days or seconds

当您只想以单个时间单位(例如天或秒)测量时间量时, ChronoUnit.between 方法很有用

LocalDate localDateStartDate = LocalDate.of(2016, 06, 10);
LocalDate localDateEndDate = LocalDate.of(2016,06,23);
long days = ChronoUnit.DAYS.between(localDateStartDate, localDateEndDate);