在Java中增加天数[重复]

时间:2022-08-25 17:35:15

This question already has an answer here:

这个问题已经有了答案:

How do I add x days to a date in Java?

如何在Java中为日期添加x天?

For example, my date is (dd/mm/yyyy) = 01/01/2012

例如,我的日期是(dd/mm/yyyy) = 01/01/2012。

Adding 5 days, the output should be 06/01/2012.

加上5天,输出应该是06/01/2012。

6 个解决方案

#1


81  

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 5); // Adding 5 days
String output = sdf.format(c.getTime());
System.out.println(output);

#2


28  

java.time

With the Java 8 Date and Time API you can use the LocalDate class.

使用Java 8 Date和Time API,您可以使用LocalDate类。

LocalDate.now().plusDays(nrOfDays)

See the Oracle Tutorial.

看到Oracle教程。

#3


19  

Calendar cal = Calendar.getInstance();    
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.YEAR, 2012);
cal.add(Calendar.DAY_OF_MONTH, 5);

You can also substract days like Calendar.add(Calendar.DAY_OF_MONTH, -5);

你也可以像Calendar.add(Calendar)一样减去天数。DAY_OF_MONTH 5);

#4


14  

Here is some simple code to give output as currentdate + D days = some 'x' date (future date):

以下是一些简单的代码,可以将输出作为currentdate + D days =某些“x”日期(未来日期):

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

Calendar c = Calendar.getInstance();    
c.add(Calendar.DATE, 5);
System.out.println(dateFormat.format(c.getTime()));

#5


12  

If you're using Joda-Time (and there are lots of good reasons to - a simple, intuitive API and thread-safety) then you can do this trivially:

如果您正在使用Joda-Time(有很多很好的理由——一个简单的、直观的API和线程安全),那么您可以做以下简单的事情:

(new LocalDate()).plusDays(5);

to give 5 days from today, for example.

例如,从今天起5天。

#6


5  

Simple, without any other API:

简单,没有任何其他API:

To add 8 days:

添加8天:

Date today=new Date();
long ltime=today.getTime()+8*24*60*60*1000;
Date today8=new Date(ltime);

#1


81  

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Now use today date.
c.add(Calendar.DATE, 5); // Adding 5 days
String output = sdf.format(c.getTime());
System.out.println(output);

#2


28  

java.time

With the Java 8 Date and Time API you can use the LocalDate class.

使用Java 8 Date和Time API,您可以使用LocalDate类。

LocalDate.now().plusDays(nrOfDays)

See the Oracle Tutorial.

看到Oracle教程。

#3


19  

Calendar cal = Calendar.getInstance();    
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.YEAR, 2012);
cal.add(Calendar.DAY_OF_MONTH, 5);

You can also substract days like Calendar.add(Calendar.DAY_OF_MONTH, -5);

你也可以像Calendar.add(Calendar)一样减去天数。DAY_OF_MONTH 5);

#4


14  

Here is some simple code to give output as currentdate + D days = some 'x' date (future date):

以下是一些简单的代码,可以将输出作为currentdate + D days =某些“x”日期(未来日期):

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

Calendar c = Calendar.getInstance();    
c.add(Calendar.DATE, 5);
System.out.println(dateFormat.format(c.getTime()));

#5


12  

If you're using Joda-Time (and there are lots of good reasons to - a simple, intuitive API and thread-safety) then you can do this trivially:

如果您正在使用Joda-Time(有很多很好的理由——一个简单的、直观的API和线程安全),那么您可以做以下简单的事情:

(new LocalDate()).plusDays(5);

to give 5 days from today, for example.

例如,从今天起5天。

#6


5  

Simple, without any other API:

简单,没有任何其他API:

To add 8 days:

添加8天:

Date today=new Date();
long ltime=today.getTime()+8*24*60*60*1000;
Date today8=new Date(ltime);