how to add days to java simple date format(如何在java简单日期格式中添加天数)
问题描述
我应该如何将 120 天添加到我使用简单日期格式获得的当前日期?
How should I add 120 days to my current date which I got using simple date format?
我看过一些关于它的帖子,但无法让它发挥作用,
I have seen few posts about it but couldn't get it to work,
我的代码如下:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
//get current date time with Date()
Date date = new Date();
我需要使用 Calendar
库还是只使用简单的日期格式?
Do I need to use the Calendar
library or can I just do it with simple date format?
推荐答案
基本上,您可以简单地使用 Calendar
,它能够根据更改自动滚动日期的各个字段例如单个字段...
Basically, you can simple use a Calendar
which has the capacity to automatically roll the various fields of a date based on the changes to a single field, for example...
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 120);
date = cal.getTime();
仔细查看 日历
了解更多详情.
Take a closer look at Calendar
for more details.
是的,有一种使用 Joda Time 的方法,但我可以更快地输入这个示例;)
Yes, there is a way to do this using Joda Time, but I could type this example quicker ;)
更新 JodaTime 示例
以下是使用 JodaTime 的示例.您可以直接使用 JodaTime 解析 String
值,但既然您已经这样做了,我就不打扰了...
The following is an example using JodaTime. You could parse the String
value directly using JodaTime, but since you've already done that, I've not bothered...
Date date = ...;
DateTime dt = new DateTime(date);
dt = dt.plusDays(120);
date = dt.toDate();
这篇关于如何在java简单日期格式中添加天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在java简单日期格式中添加天数


- 如何指定 CORS 的响应标头? 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 转换 ldap 日期 2022-01-01
- 获取数字的最后一位 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01