How to add one year to current date in groovy script?
def Format1 = "yyyy-MM-dd"
def today = new Date()
def currentDate = today.format(Format1)
Example : 2015-07-29 to 2016-07-29 and 2015-07-29 to 2015-10-29.
public String format(String format, TimeZone tz)Create a String representation of this date according to the given format pattern and timezone. For example: def d = new Date(0) def tz = TimeZone. getTimeZone('GMT') println d. format('dd/MMM/yyyy', tz) would return the string "01/Jan/1970" .
use(groovy. time. TimeCategory) { def duration = date1 - date2 print "Days: ${duration. days}, Hours: ${duration.
String newDate = Date. parse('MM/dd/yyyy',dt). format("yyyy-MM-dd'T'HH:mm:ss'Z'");
Use TimeCategory
.
import groovy.time.TimeCategory
def acceptedFormat = "yyyy-MM-dd"
def today = new Date() + 1
def currentdate = today.format(acceptedFormat)
use(TimeCategory) {
def oneYear = today + 1.year
println oneYear
def ninetyDays = today + 90.days
println ninetyDays
}
More information on how this works can be found in the documentation on The Groovy Pimp my Library Pattern. In short, the Integer
class is enriched in the use
block, providing it with extra methods that make date manipulation very convenient.
Do note that the +
(or plus
) operator already works with regular integers, but the default is then to add one day. (As such, new Date() + 1
will get you the date in 24 hours)
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