How to add a date to the java simple date format

How can I add 120 days to the date I currently use simple date format?

I've read very few posts about it, but I can't make it work,

My code is as follows:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
//get current date time with Date()
Date date = new Date();

Do I need to use a calendar library, or can I use a simple date format?

Solution

Basically, you can simply use a calendar that automatically scrolls the fields of the date based on changes to a single field, such as

Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,120);
date = cal.getTime();

Check calendar for more details

Yes, there is a way to do this using joda time, but I can enter this example faster;)

Update using the jodatime sample

The following is an example of using jodatime You can use jodatime to parse string values directly, but now that you have done so, I won't bother

Date date = ...;
DateTime dt = new DateTime(date);
dt = dt.plusDays(120);
date = dt.toDate();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>