今天正好是3月,可以測試特殊月份的2月
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8")); // 設置時區
SimpleDateFormat oSdf = new SimpleDateFormat ("yyyy-MM-dd");
String format = oSdf.format(new Date());
cal.setTime(oSdf.parse(format)); // 設置時間
int thisMonth = cal.get(Calendar.DAY_OF_MONTH); // 得到當月
cal.add(Calendar.MONTH, -1); // 得到上月月份
int lastMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 得到上月總天數