本文介绍了如何在日期中添加一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为特定日期添加一天.我该怎么做?

I want to add one day to a particular date. How can I do that?

Date dt = new Date();

现在我想在这个日期上加一天.

Now I want to add one day to this date.

推荐答案

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

Given a Date dt you have several possibilities:

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

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

解决方案 2:您应该认真考虑使用 Joda-时间库,由于Date类的种种缺点.使用 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):

Date dt = new Date();
LocalDateTime.from(dt.toInstant()).plusDays(1);

这篇关于如何在日期中添加一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 16:49