java学习之日期的运算代码
来源:文书网 1.18W
java学习之日期的运算代码复制代码 代码如下:
import leDateFormat;
import ndar;
import ;
public class HelloWorld {
public static void main(String[] args) {
//指定时间输出格式
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date dt = new Date();
tln("当前时间:" + at(dt));
Calendar rightNow = nstance();
ime(dt);
(,-1);//当时日期减1年
tln("减1年:" + at(ime()));
(H,3);//(在刚才的结果上)再加3个月
tln("再加3个月:" + at(ime()));
(_OF_YEAR,10);//(在刚才的结果上)再加10天
tln("再加10天:" + at(ime()));
}
}
输出结果:
当前时间:2013-02-21 09:40:49
减1年:2012-02-21 09:40:49
再加3个月:2012-05-21 09:40:49
再加10天:2012-05-31 09:40:49