java 如何得到系统当前的时间减去五年的时间呢

2024-11-14 22:33:17
推荐回答(3个)
回答1:

通过时间戳的差值来计算,给你一个例子:
======================
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));

回答2:

试试,
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

回答3:

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()));