-4

I want to make a java program that takes dates as input and generate another date based on the number of days I will add.

eg:

    input date 23/4/2018
    number of days: 10 
    output date: 3/5/2018 
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • https://stackoverflow.com/questions/12087419/adding-days-to-a-date-in-java Take a look here – Sonny Hansen Dec 05 '18 at 14:05
  • You need `LocalDate` and `DateTimeFormatter`. Learn about them in [the Oracle tutorial: date time](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 05 '18 at 14:20
  • 1
    Welcome to Stack Overflow. I didn’t downvote your question. I guess the downvotes were because it seems poorly researched. You are supposed to search and research before posting a question, and also in your question report what you have found and how it fell short of solving your problem. Also your question is pretty broad (and might have been closed for that reason). Stack Overflow works best with very concrete and specific questions. Such typically include code you have already written and with which you are having a problem. – Ole V.V. Dec 05 '18 at 14:29

1 Answers1

0

Note; this is pre java 8 code. for java 8 or newer use LocalDate and Period.

   int x= <yournumber of days>


Calendar calendar = Calendar.getInstance();
calendar.setTime(givenDate);
calendar.add(Calendar.DAY, x);

use a switch or if-statements to check wether they are days, months or years. take a look around SO. this question has been answered before.

Daan
  • 373
  • 2
  • 9
  • 1
    Since we have [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) I’d prefer not to use the old and poorly deigned `Calendar` class. – Ole V.V. Dec 05 '18 at 14:23
  • 1
    fair point. Except i havent worked with java in a while and dont know the ins and outs of the new LocalDate and Period classes, the OP might want to check those though – Daan Dec 05 '18 at 14:25
  • 1
    updated my answer to reflect this. – Daan Dec 05 '18 at 14:26
  • 1
    @Daan Most of the *java.time* functionality is available for Java 6 and Java 7 in the *ThreeTen-Backport* library. No need to ever again use those bloody awful old date-time classes like `Calendar`. – Basil Bourque Dec 05 '18 at 16:45