如何获得今天的日期和当月的最后日期之间的天数? [英] How to get number of days between today's date and last date of the current month?
本文介绍了如何获得今天的日期和当月的最后日期之间的天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我正在制作一个Android应用,我需要计算当前日期和当前月份的最后日期之间的天数.
So I am making an Android app and I need to do calculations on number of days between current date and last date of current month.
假设今天是1/11/2017,本月的最后日期是30/11/2017.因此,天数将为30.因为30天之后,下个月将开始.
Suppose today is 1/11/2017 and last date of current month is 30/11/2017. So number of days would 30. Because after 30 days next month will start.
推荐答案
Calendar calendar = Calendar.getInstance();
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
int daysLeft = lastDay - currentDay;
System.out.println("Last Day: " + lastDay);
System.out.println("Current Day : " + currentDay);
System.out.println("There are " + daysLeft + " days left in the month.");
输出
Last Day: 30
Current Day : 1
There are 29 days left in the month.
这篇关于如何获得今天的日期和当月的最后日期之间的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文