1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.Calendar; 4 import java.util.Date; 5 6 7 public class TestMinusOneMonth { 8 9 public static void main(String[] args) { 10 String string = "2014-01-01"; 11 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 12 try { 13 Date date = sdf.parse(string); 14 Calendar calendar = Calendar.getInstance(); 15 calendar.setTime(date); 16 calendar.add(Calendar.MONTH, -1); 17 date = calendar.getTime(); 18 string = sdf.format(date); 19 System.out.println(string); //2013-12-01 20 } catch (ParseException e) { 21 e.printStackTrace(); 22 } 23 } 24 }
时间: 2024-10-28 14:45:11