如何添加日期到java简单日期格式 [英] how to add days to java simple date format

查看:114
本文介绍了如何添加日期到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();

我需要使用日历

推荐答案

基本上,你可以简单地使用日历,它能够根据单个字段的更改自动滚动日期的各个字段,例如...

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时间,但我可以更快地输入这个例子;)

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简单日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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