有没有在安卓中以整数形式获取天数的简单方法?

huangapple go评论72阅读模式
英文:

Is there a simple way to get days as Integers in android?

问题

我正在尝试制作一个每日提醒。例如,我想要知道从任务开始过去了多少天。还有一件很重要的事情,我希望天数是整数(例如,天数大于0,天数大于1),这样我可以将它们放入一个ArrayList中以便以后使用。有没有什么好的方法可以做到这一点?我不想使用计数器之类的东西。

英文:

I am trying to make a daily reminder. For example, I want to know how many days have passed from a task. One more important thing, I want days to be integers (e.g. day->0 day->1), so I can put them in an Arraylist and use it later.
Is there any good way to do this? I don't want something like counter or things like that.

答案1

得分: 1

你可以尝试使用 java.time.temporal.ChronoUnit 中的辅助方法,示例实现可以在 https://beginnersbook.com/2017/10/java-8-calculate-days-between-two-dates/ 找到。

要计算两个日期之间的天数,我们可以使用 java.time.temporal.ChronoUnitDAYS.between() 方法。

long noOfDaysBetween = DAYS.between(startDate, endDate);
// 或者可以选择以下另一种方式
long noOfDaysBetween = startDate.until(endDate, DAYS);

在计算 noOfDaysBetween 时startDate 包含在内而 endDate 不包含
英文:

You can try helpers from java.time.temporal.ChronoUnit example implementation can be found https://beginnersbook.com/2017/10/java-8-calculate-days-between-two-dates/.

To calculate the days between two dates we can use the DAYS.between() method of java.time.temporal.ChronoUnit.

long noOfDaysBetween = DAYS.between(startDate, endDate);
// or alternatively
long noOfDaysBetween = startDate.until(endDate, DAYS);

(The startDate is Inclusive and endDate is Exclusive in the calculation of noOfDaysBetween)

huangapple
  • 本文由 发表于 2020年8月25日 22:34:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63581214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定