Why I'm getting 31 instead of 3? Now December, so previous of Nov = 30.
Calendar calendarPreviousMonth = Calendar.getInstance();
calendarPreviousMonth.add(Calendar.MONTH, calendarCurrent.get(Calendar.MONTH) - 1);
System.out.println("Amount of days in month " + calendarPreviousMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
Thank you!
查找上个月的代码应如下:
calendarPreviousMonth.add(Calendar.MONTH, -1);
This is wrong:
calendarPreviousMonth.add(Calendar.MONTH, calendarCurrent.get(Calendar.MONTH)-1)
You either want to add -1
to month or set it to calendarCurrent.get(Calendar.MONTH) - 1
. Not both! Try this:
calendarPreviousMonth.add(Calendar.MONTH, -1);
or this:
calendarPreviousMonth.set(Calendar.MONTH, calendarCurrent.get(Calendar.MONTH)-1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.