import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Date;
class TimeSub
{
public static void main(String[] args)
{
Date now = new Date();
SimpleDateFormat time=new SimpleDateFormat("yyyyMMdd");
System.out.println(time.format(now ));
int year_int=(new java.lang.Integer(time.format(now).substring(0,4))).intValue();
int month_int=(new java.lang.Integer(time.format(now).substring(4,6))).intValue()-1;/*这里月份要减一,因为,java里面0表示一月份*/
int day_int=(new java.lang.Integer(time.format(now).substring(6,8))).intValue();
GregorianCalendar worldTour = new GregorianCalendar(year_int,month_int,day_int);
//worldTour.add(7,-200);/*这里7标识安日偏移,1是年,2是月,负号标识向前偏移200天*/
worldTour.add(7,-1);/*这里7标识安日偏移,1是年,2是月,负号标识向前偏移200天*/
SimpleDateFormat time1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
Date d = worldTour.getTime();
System.out.println(time1.format(d));
}
}
取得当前时间:
Date now = new Date();
SimpleDateFormat time=new SimpleDateFormat("yyyyMMdd");
System.out.println(time.format(now ));
然后把现在时间的年,月,日转化成整数;
int year_int=2006
int month_int=11
int day_int=8
GregorianCalendar worldTour = new GregorianCalendar(year_int,month_int,day_int);
worldTour.add(7,-200);/*这里7标识安日偏移,1是年,2是月,负号标识向前偏移200天*/
SimpleDateFormat time1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
Date d = worldTour.getTime();
System.out.println(time1.format(d););
当前时间:
public static Date getNowDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString=formatter.format(currentTime);
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR,200);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = sdf.format(cal.getTime());