我使用的日期格式为:yyyy-mm-dd。
如何将此日期增加一天?
我使用的日期格式为:yyyy-mm-dd。
如何将此日期增加一天?
当前回答
在java 8中,您可以使用java.time. localdate
LocalDate parsedDate = LocalDate.parse("2015-10-30"); //Parse date from String
LocalDate addedDate = parsedDate.plusDays(1); //Add one to the day field
您可以将其转换为java.util.Date对象,如下所示。
Date date = Date.from(addedDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
您可以将LocalDate格式化为字符串,如下所示。
String str = addedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
其他回答
像这样的东西应该可以达到目的:
String dt = "2008-01-01"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1); // number of days to add
dt = sdf.format(c.getTime()); // dt is now the new date
看看Joda-Time (https://www.joda.org/joda-time/)。
DateTimeFormatter parser = ISODateTimeFormat.date();
DateTime date = parser.parseDateTime(dateString);
String nextDay = parser.print(date.plusDays(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
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更改为另一个数字,以获得适当的增量。
startCalendar.add(Calendar.DATE, 1); //Add 1 Day to the current Calender