Java 由當前年月得到前一個年月

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM");
String payoffYearMonth = "2018-01";
Date currdate = null;
try {
      currdate = sd.parse(payoffYearMonth);
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(currdate);
      calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
      System.out.println(sd.format(calendar.getTime()));
} catch (ParseException e) {
  e.printStackTrace();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章