如何获得今天的日期和当月的最后日期之间的天数?
问题描述:
因此,我正在制作一个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.