获取上个月第一天的方法:
Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, -1); calendar.set(Calendar.DAY_OF_MONTH, 1); System.out.println(calendar.get(Calendar.YEAR)); System.out.println(calendar.get(Calendar.MONTH));
获取上个月最后一天的方法,这个稍微要变通一下,先将日期设置为本月的第一天,然后减去一天就变成了上个月的最后一天了:
Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DATE, -1); System.out.println(calendar.get(Calendar.YEAR)); System.out.println(calendar.get(Calendar.MONTH));
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/14579.html