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());
当前时间: 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; }
取得当前时间: 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););
然后把现在时间的年,月,日转化成整数; int year_int=2006 int month_int=11 int day_int=8这里还是自己定义的时间呀 能否根据获得的当前时间自动计算出前200天的时间? 而且 worldTour.add(7,-200);这里也有错误 如果用 worldTour.add(7,-1);的话将不是得到 11.7号
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)); } }
int year_int=2006 int month_int=11 int day_int=8 这个值传到java里面,表示的是20061208.,他的月份是从0开始的. 我疏忽了,不好意思!上面我写了一个完整的代码,而且测试通过!
cal.add(Calendar.DAY_OF_YEAR,200);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = sdf.format(cal.getTime());
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;
}
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););
int year_int=2006
int month_int=11
int day_int=8这里还是自己定义的时间呀 能否根据获得的当前时间自动计算出前200天的时间?
而且 worldTour.add(7,-200);这里也有错误 如果用 worldTour.add(7,-1);的话将不是得到 11.7号
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));
}
}
int month_int=11
int day_int=8
这个值传到java里面,表示的是20061208.,他的月份是从0开始的.
我疏忽了,不好意思!上面我写了一个完整的代码,而且测试通过!