-5

Hello I want to add 3 days to the current day. I have seen some ways but they are using a different format. I have this:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
String date =DateTimeFormatter.ofPattern("yyyy/MM/dd").format(localDate);

How can i add 3 days to that?

PD: take in account that i will use these dates to do a select from a database

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
19mike95
  • 506
  • 2
  • 4
  • 19
  • 1
    Did you do any research prior to asking? A search? Checking the [documentation of `LocalDate`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html)? Sometimes you can find your answer faster that way than waiting for someone to type it here. – Ole V.V. May 20 '17 at 12:52
  • See for example [this answer: Adding days to a date in Java](http://stackoverflow.com/a/23438360/5772882). – Ole V.V. May 20 '17 at 12:59
  • 1
    Search Stack Overflow before posting. Always assume that basic questions on common topics have already been asked and answered. – Basil Bourque May 20 '17 at 22:19

2 Answers2

6

What about

localDate = localDate.plusDays(3);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • when using this I get this error: This exception may happen if you add integers representing units of time directly to datetime values using the arithmetic plus operator but without specifying the unit of date. In this specific case you have to use, for example, + 1 DAY. – 19mike95 May 20 '17 at 12:41
  • 1
    That sounds like an unrelated error - one to do with the database - and one for a new question.... – Reimeus May 20 '17 at 12:45
  • @19mike95 I think you've misunderstood how to apply this answer. Put this line of code between the second and third lines that you quoted above. That is, this is an entirely Java calculation. Don't try to turn it into SQL. – Dawood ibn Kareem May 21 '17 at 00:46
3

Have a look at the plusDays(long daysToAdd)method of the LocalDate class. That should help you achieve what you wish to do.

steven
  • 110
  • 5
  • Since I can't comment on the post made by @Reimeus I have to reply to @19mike95 this way. Did you try it the following way as well? `LocalDate localDate = LocalDate.now().plusDays(3);` ? Might be worth a shot... Otherwise it's like @Reimeus says and the exception is related to database. – steven May 20 '17 at 13:30