首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

period.getDays()在android中每次返回0

在Android开发中,如果你使用的是java.time.Period类来计算两个日期之间的天数,并且发现Period.getDays()每次都返回0,这可能是因为Period类的设计。Period类的getDays()方法返回的是两个日期之间的天数部分,而不是总天数。

例子

假设你有两个日期:2023-01-012023-01-10。如果你使用Period类来计算它们之间的差异:

代码语言:javascript
复制
LocalDate startDate = LocalDate.of(2023, 1, 1);
LocalDate endDate = LocalDate.of(2023, 1, 10);
Period period = Period.between(startDate, endDate);

int days = period.getDays(); // 这将返回0,因为没有完整的月份或年份差异

在这个例子中,period.getDays()返回的是两个日期之间的天数部分,而不是总天数。因为这两个日期之间没有完整的月份或年份差异,所以getDays()返回0。

解决方法

如果你想要计算两个日期之间的总天数,可以使用ChronoUnit.DAYS.between()方法:

代码语言:javascript
复制
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

LocalDate startDate = LocalDate.of(2023, 1, 1);
LocalDate endDate = LocalDate.of(2023, 1, 10);

long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
System.out.println("Days between: " + daysBetween); // 这将返回9

完整示例

以下是一个完整的示例,展示了如何使用ChronoUnit.DAYS.between()来计算两个日期之间的总天数:

代码语言:javascript
复制
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2023, 1, 1);
        LocalDate endDate = LocalDate.of(2023, 1, 10);

        long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
        System.out.println("Days between: " + daysBetween); // 这将返回9
    }
}

通过使用ChronoUnit.DAYS.between()方法,你可以准确地计算两个日期之间的总天数,而不是仅仅获取天数部分。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • 模拟赛 2018 Benelux Algorithm Programming Contest (BAPC 18)(部分题)

    After the festive opening of your new store, the Boutique store for Alternative Paramedicine and Cwakhsahlvereigh, to your disappointment you find out that you are not mak- ing as many sales as you had hoped. To remedy this, you decide to run a special offer: you will mark some subset of the n items for sale in your store as participating in the offer, and if people buy exactly two of these items, and the cost of these items is strictly more than X euros, you will give them a free complimentary unicorn horn! Since you recently found out all your unicorn horns are really narwahl tusks, you decide to rig the offer by picking the participating items in such a way that no one can earn a horn anyway. To make sure no one becomes suspicious, you want to mark as many items as possible as participating in the offer. Input • On the first line two integers, 1 ≤ n ≤ 10 5 , the number of items for sale in your store, and 1 ≤ X ≤ 10 9 , the minimum cost specified in the statement. • On the second line n positive integers, each at most 10 9 . These are the prices of the items in the store. Output Print the maximum number of items you can mark as part of your special offer, without anyone actually being able to receive a horn. Sample Input 1 Sample Output 1 5 6 1 2 3 4 5 3 Sample Input 2 Sample Output 2 5 10 4 8 1 9 7 2 Sample Input 3 Sample Output 3 4 10 1 3 1 7 4 4 Problem A: A Prize No One Can Win Sample Input 4 Sample Output 4 1 5 6 1

    04
    领券