java获取指定年月的上月日期

java 获取指定月的上一月

String rq = "2017-10";

DateFormat format2 =  new SimpleDateFormat("yyyy-MM");
Date date = new Date();
try{
    date = format2.parse(rq);
}catch(Exception e){
    e.printStackTrace();
}
Calendar c = Calendar.getInstance();
c.setTime(date);
SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM");
c.add(Calendar.MONTH, -1);
String time = format.format(c.getTime());

System.out.println(time);

你可能感兴趣的:(java)