我使用的日期格式为:yyyy-mm-dd。
如何将此日期增加一天?
我使用的日期格式为:yyyy-mm-dd。
如何将此日期增加一天?
当前回答
自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));
其他回答
我认为最快的一个,永远不会被弃用,它是一个深入核心的
let d=new Date();
d.setTime(d.getTime()+86400000);
console.log(d);
只有一行, 只有两个命令。 它工作在日期类型,不使用日历。
我一直认为最好在代码端使用unix时间,并在准备好显示给用户时显示日期。
要打印日期d,我使用
let format1 = new Intl.DateTimeFormat('en', { year: 'numeric', month: 'numeric', month: '2-digit', day: '2-digit'});
let [{ value: month },,{ value: day },,{ value: year }] = format1.formatToParts(d);
它可以设置月、年和日,但可以扩展到小时、分钟和秒,也可以根据国家国旗在标准表示中使用。
看看Joda-Time (https://www.joda.org/joda-time/)。
DateTimeFormatter parser = ISODateTimeFormat.date();
DateTime date = parser.parseDateTime(dateString);
String nextDay = parser.print(date.plusDays(1));
startCalendar.add(Calendar.DATE, 1); //Add 1 Day to the current Calender
如果你使用的是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);
}
请注意这一行增加了24小时:
d1.getTime() + 1 * 24 * 60 * 60 * 1000
但是这条线增加了一天
cal.add( Calendar.DATE, 1 );
在夏令时改变的日子里(25或23小时),你会得到不同的结果!