英文:
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.ChronoUnit
的 DAYS.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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论