我使用的日期格式为:yyyy-mm-dd。
如何将此日期增加一天?
我使用的日期格式为:yyyy-mm-dd。
如何将此日期增加一天?
当前回答
试试这个方法:
public static Date addDay(int day) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE, day);
return calendar.getTime();
}
其他回答
构造一个Calendar对象并调用add(Calendar。目前为止,1);
在Java 8中简单的方法是:
Date.from(Instant.now().plusSeconds(SECONDS_PER_DAY))
Date today = new Date();
SimpleDateFormat formattedDate = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1); // number of days to add
String tomorrow = (String)(formattedDate.format(c.getTime()));
System.out.println("Tomorrows date is " + tomorrow);
这将给出明天的日期。C.add(…)参数可以从1更改为另一个数字,以获得适当的增量。
其实很简单。 一天包含86400000毫秒。 所以首先你从系统中通过使用System. currenttimemillis()获得当前时间,单位是millis 添加8000000毫秒,并使用日期类生成以毫秒为单位的日期格式。
例子
String Today = new Date(System.currentTimeMillis()).toString();
今天是2019-05-9
String明天=新的日期(System.currentTimeMillis() + 86400000).toString();
明天将是2019-05-10
最新消息。
字符串后天将是2019-05-11
只需在字符串中传递日期和接下来的天数
private String getNextDate(String givenDate,int noOfDays) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String nextDaysDate = null;
try {
cal.setTime(dateFormat.parse(givenDate));
cal.add(Calendar.DATE, noOfDays);
nextDaysDate = dateFormat.format(cal.getTime());
} catch (ParseException ex) {
Logger.getLogger(GR_TravelRepublic.class.getName()).log(Level.SEVERE, null, ex);
}finally{
dateFormat = null;
cal = null;
}
return nextDaysDate;
}