如何计算一段时间内的天数?

时间:2022-08-25 18:51:18

For the following Period calculation:

对于以下期间计算:

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.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天(8月)+ 30天(9月)+ 1(10月)= 62天

Is there a method in the java.time package 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包中是否有一个方法可以给出一个Period中的天数?我找不到一个。不确定我是否忽略了任何东西,或者它是否只是在那里。

3 个解决方案

#1


57  

From the documentation:

从文档:

To define an amount of time with date-based values (years, months, days), use the Period class. The Period class 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.between method.

要使用基于日期的值(年,月,日)定义时间量,请使用Period类。 Period类提供了各种get方法,例如getMonths,getDays和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)");

The code produces output similar to the following:

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

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

#2


8  

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

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

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.between as Saket Mittal writes.

如果你需要两个日期之间的天数,你应该使用ChronoUnit.DAYS.between作为Saket Mittal写道。

#3


3  

There's a specific object depending at the amount of time you'd like to deal with. This page here is 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);

#1


57  

From the documentation:

从文档:

To define an amount of time with date-based values (years, months, days), use the Period class. The Period class 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.between method.

要使用基于日期的值(年,月,日)定义时间量,请使用Period类。 Period类提供了各种get方法,例如getMonths,getDays和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)");

The code produces output similar to the following:

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

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

#2


8  

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

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

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.between as Saket Mittal writes.

如果你需要两个日期之间的天数,你应该使用ChronoUnit.DAYS.between作为Saket Mittal写道。

#3


3  

There's a specific object depending at the amount of time you'd like to deal with. This page here is 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);