java取月份的天数,对字符串的处理
1.使用Calendar获取上个月的最后一天
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DATE, -1);
String lastMonthEndDay = new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
2.获取自定义月份的天数
String sumDate = "201802";
Calendar calendar = Calendar.getInstance();
try {
Date data = new SimpleDateFormat("yyyyMM").parse(sumDate);
calendar.setTime(data);
} catch (ParseException e) {
e.printStackTrace();
}
int MonthDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(MonthDays);
3.关于subString
String str = "abcdefg";
System.out.println(str.substring(0, 3));
输出结果为abc
首先截取字符串是从0开始计算,然后截取到3.
截取的内容为 0 1 2 ,也就是a b c