0

Get the current date and add 30 days form the date and stored in the variable named reminderDueDate

def date = new Date().plus(30)
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
reminderDueDate =  simpleDateFormat .format(date)

from that variable, I need to minus the days Let me know how?

say for example
reminderDueDate = 2019-09-07

I need to minus 7 days from the reminderDueDate how?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Prabu
  • 3,550
  • 9
  • 44
  • 85
  • 1
    after `.format` it's not a date anymore. `reminderDueDate` is a string. so you have to convert string to a date, then apply minus, then convert back to string. – daggett Aug 08 '19 at 08:54
  • 1
    btw in groovy you could use Date.format() to convert date to string, and Date.parse() to convert string to date. http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html – daggett Aug 08 '19 at 08:56
  • `.plus(-7)` maybe? – cfrick Aug 08 '19 at 09:30
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 09 '19 at 10:34
  • Also please keep your model and your user interface apart. In your model store `reminderDueDate` as a `LocalDate`, not as a `String`. That makes everything simpler and easier to grasp. Only when you need to show a date to the user, format it into a `String` for that purpose. – Ole V.V. Aug 09 '19 at 10:36

1 Answers1

0

SimpleDateFormat is a class that comes with Java SDK. it converts date in its object represenetation to string.

So after calling reminderDueDate = simpleDateFormat .format(date) you'll have a string that looks in compliance with a specified pattern in simple date format object constructor. Of course you can't add days to a string it doesn't make sense.

You can get the date back by calling:

d = Date.parse('yyyy-MM-dd', reminderDueDate)
// and now:
d.minus(1)

However, as a side note, I believe this API is pretty old and since Java 8 comes with much simpler API to use I suggest using LocalDate instead.

Here you'll find information about converting LocalDate to String for example.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97