我使用的日期格式为:yyyy-mm-dd。
如何将此日期增加一天?
我使用的日期格式为:yyyy-mm-dd。
如何将此日期增加一天?
当前回答
只需在字符串中传递日期和接下来的天数
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;
}
其他回答
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更改为另一个数字,以获得适当的增量。
构造一个Calendar对象并调用add(Calendar。目前为止,1);
自Java 1.5以来,TimeUnit.DAYS.toMillis(1)对我来说看起来更干净。
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
Date day = dateFormat.parse(string);
// add the day
Date dayAfter = new Date(day.getTime() + TimeUnit.DAYS.toMillis(1));
投票最多的答案是使用传统java。util date-time API,这在2009年被问到这个问题时是正确的。2014年3月,java。时间API取代了容易出错的传统日期时间API。从那时起,强烈建议使用这个现代日期-时间API。
我使用的日期格式为:yyyy-mm-dd
不管您使用的是传统的解析/格式化API还是现代的API,您都使用了错误的字母。字母m用于表示小时的分钟,而表示月份的正确字母是m。
“yyyy-MM-dd”为java.time.LocalDate的默认格式
java。时间API基于ISO 8601标准,因此如果日期-时间字符串已经是ISO 8601格式,则不需要显式指定DateTimeFormatter来解析日期-时间字符串。类似地,java的toString实现。time类型返回ISO 8601格式的字符串。查看LocalDate#parse和LocalDate#toString了解更多信息。
将本地日期增加一天的方法
有三种选择:
LocalDate # plusDays(长daysToAdd) LocalDate#plus(long amountToAdd, TemporalUnit unit):它有一些额外的功能,例如,你可以使用它来增加一个本地日期的天,周,月,年等。 LocalDate#plus(TemporalAmount amountToAdd):您可以指定一个Period(或任何其他实现TemporalAmount的类型)来添加。
演示:
import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
// Parsing
LocalDate ldt = LocalDate.parse("2020-10-20");
System.out.println(ldt);
// Incrementing by one day
LocalDate oneDayLater = ldt.plusDays(1);
System.out.println(oneDayLater);
// Alternatively
oneDayLater = ldt.plus(1, ChronoUnit.DAYS);
System.out.println(oneDayLater);
oneDayLater = ldt.plus(Period.ofDays(1));
System.out.println(oneDayLater);
String desiredString = oneDayLater.toString();
System.out.println(desiredString);
}
}
输出:
2020-10-20
2020-10-21
2020-10-21
2020-10-21
2020-10-21
如何从遗留API切换到现代日期时间API?
您可以在java-util-date实例上使用dat# toInstant从传统日期-时间API切换到现代日期-时间API。一旦拥有了Instant,就可以轻松获得其他日期-时间类型的java。时间的API。一个瞬间表示时间上的一个时刻,与时区无关,即它表示UTC的日期-时间(通常显示为Z,代表Zulu-time, ZoneOffset为+00:00)。
演示:
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
Instant instant = date.toInstant();
System.out.println(instant);
ZonedDateTime zdt = instant.atZone(ZoneId.of("Asia/Kolkata"));
System.out.println(zdt);
OffsetDateTime odt = instant.atOffset(ZoneOffset.of("+05:30"));
System.out.println(odt);
// Alternatively, using time-zone
odt = instant.atZone(ZoneId.of("Asia/Kolkata")).toOffsetDateTime();
System.out.println(odt);
LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Kolkata"));
System.out.println(ldt);
// Alternatively,
ldt = instant.atZone(ZoneId.of("Asia/Kolkata")).toLocalDateTime();
System.out.println(ldt);
}
}
输出:
2022-11-12T12:52:18.016Z
2022-11-12T18:22:18.016+05:30[Asia/Kolkata]
2022-11-12T18:22:18.016+05:30
2022-11-12T18:22:18.016+05:30
2022-11-12T18:22:18.016
2022-11-12T18:22:18.016
从Trail: Date Time了解更多关于现代Date-Time API的信息。
如果你使用的是Java 8, Java .time. localdate和Java .time.format. datetimeformatter可以让这个工作变得非常简单。
public String nextDate(String date){
LocalDate parsedDate = LocalDate.parse(date);
LocalDate addedDate = parsedDate.plusDays(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd");
return addedDate.format(formatter);
}