Java计算当前日期加一天,减一天,下图是方法及输出结果,注释写的很明白喽

代码,要的可以复制:
public static void main(String[] args)
throws ParseException{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//取今天日期,如果日期类型为String类型,可以使用df.parse()方法,转换为Date类型
Date date = new Date();
Calendar calendar = Calendar.getInstance();//new一个Calendar类,把Date放进去
calendar.setTime(date);
calendar.add(Calendar.DATE, 1);//实现日期加一操作,也就是明天
//控制台打印的日期为明天日期,2019-06-11
System.out.println("明天的日期为:" + df.format(calendar.getTime()));
//此时的日期为明天的日期,要实现昨天,日期应该减二
calendar.add(Calendar.DATE, -2);
System.out.println("昨天的日期为:" + df.format(calendar.getTime()));
}
本文介绍了一种在Java中进行日期加减的方法,通过使用SimpleDateFormat和Calendar类,实现了当前日期加一天和减一天的功能,并提供了完整的代码示例。
662

被折叠的 条评论
为什么被折叠?



