学习自:https://blog.csdn.net/dongyuxu342719/article/details/78131697
开发时遇到的,十二个月就到二月份有问题,刚开始以为是自己改时间戳整的,结果发现并不是,后加了一段代码解决(图中!!!!就是),好像意思是清缓存。
public Long getLastDayOfMonth(int year,int month){ Long lastDays =0l; Calendar cal = Calendar.getInstance(); cal.clear(); //!!!!!!!!!!!!!!!!!!!!!!!!! //设置年份 cal.set(Calendar.YEAR,year); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最大天数 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //设置日历中月份的最大天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String lastDayOfMonth = sdf.format(cal.getTime()); try { lastDays = sdf.parse(lastDayOfMonth).getTime(); } catch (ParseException e) { e.printStackTrace(); } return lastDays; }