通过时间戳的差值来计算,给你一个例子:
======================
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
//现在时刻:
System.out.println(format.format(date));
long nowTime=date.getTime();
long year=5;
nowTime=nowTime-year*365*24*60*60*1000;
date=new Date(nowTime);
//这就是5年前的现在!!!
System.out.println(format.format(date));
试试,
Format f = new SimpleDateFormat("yyyy-MM");
Calendar c = Calendar.getInstance();
System.out.println("之前:" + f.format(c.getTime()));
c.add(Calendar.YEAR, -4);
System.out.println("之后:" + f.format(c.getTime()));
结果:
之前:2012-09
之后:2008-09
Format f = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = new GregorianCalendar();
c.add(Calendar.YEAR, -4);
c.set(Calendar.DAY_OF_YEAR, 1);
System.out.println(f.format(c.getTime()));