且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何添加一天的日期?

更新时间:2021-11-11 08:48:42

给定一个 Date dt 你有几种可能性:

Given a Date dt you have several possibilities:

解决方案1:您可以使用日历类:

Date dt = new Date();
Calendar c = Calendar.getInstance(); 
c.setTime(dt); 
c.add(Calendar.DATE, 1);
dt = c.getTime();

解决方案2:您应该认真考虑使用 Joda-Time库 ,因为的各种缺点日期类。使用Joda-Time,您可以执行以下操作:

Solution 2: You should seriously consider using the Joda-Time library, because of the various shortcomings of the Date class. With Joda-Time you can do the following:

Date dt = new Date();
DateTime dtOrg = new DateTime(dt);
DateTime dtPlusOne = dtOrg.plusDays(1);

解决方案3:使用 Java 8 也可以使用新的 JSR 310 API(灵感来自Joda-Time):

Solution 3: With Java 8 you can also use the new JSR 310 API (which is inspired by Joda-Time):

LocalDateTime.from(dt.toInstant()).plusDays(1);