I need to get a date by subtracting a number from current date in MM/dd/yyyy format
I got the current date by using new Date().format("MM/dd/yyyy")
Please help me with a function that subtracts 1,2 to the above date and produces a date in MM/dd/yyyy format
I have tried
def today = new Date().format("MM/dd/yyyy")
def yesterday = today -1
println today
println yesterday
which gives me
01/11/2012
0/11/2012
[:] creates an empty Map. The colon is there to distinguish it from [] , which creates an empty List. This groovy code: def foo = [:]
String newDate = Date. parse('MM/dd/yyyy',dt). format("yyyy-MM-dd'T'HH:mm:ss'Z'");
You are subtracting from a String
try:
def today = new Date()
def yesterday = today - 1
println today.format("MM/dd/yyyy")
println yesterday.format("MM/dd/yyyy")
Groovy comes with some really useful methods for manipulating dates you can use .previous() for the day before and .next() for the day after.
def today = new Date()
def yesterday = today.previous()
println today.format("MM/dd/yyyy")
println yesterday.format("MM/dd/yyyy")
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With