赞
踩
转自:微点阅读 https://www.weidianyuedu.com
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* JAVA的日期加减方法
*
* @author 品略网(品略个人图书馆,m.pinlue.com)
*/
public class Test {
public static void main(String[] args) throws Exception {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));// 今天的日期
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);// 让日期加1
System.out.println(calendar.get(Calendar.DATE));// 加1之后的日期Top
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("今天的日期:" + df.format(d));
System.out.println("两天前的日期:" + df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000)));
System.out.println("三天后的日期:" + df.format(new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000)));
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。