如何添加一天的日期? [英] How to add one day to a date?

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

问题描述

我想在特定日期添加一天。我该怎么做?

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:您可以使用日历类:

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);

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆