Want to display 2year less date from current date

Hi All,

I am trying to display the 2 year less date in the console in MM/DD/YYYY format and here is the code:
def acceptedFormat = “MM/dd/yyyy”
def today1 = new Date()
def currentdate = today1.format(acceptedFormat)

println(currentdate)

use(TimeCategory) {
def twoYear = currentdate - 2.year
println("Minus 2year: " + twoYear)

 def twoYear = currentdate - 2.year
 println("Minus 2year: " + twoYear)

}

     def twoYear = currentdate - 2.year
 println("Minus 2year: " + twoYear)

above statement is displaying the current date but format is correct
output in console: Minus 2year: 09/03/2019
def twoYear = currentdate - 2.year
println("Minus 2year: " + twoYear)
This statement is displaying the correct date but not sure how to apply the format MM/DD/YYYY
output in console: Minus 2year: Sun Sep 03 10:01:20 PDT 2017

please help me!!!

please ignore the above post and here is the actual one.

I am trying to display the 2 year less date in the console in MM/DD/YYYY format and here is the code:

def acceptedFormat = “MM/dd/yyyy”
def today1 = new Date()
def currentdate = today1.format(acceptedFormat)

println(currentdate)

use(TimeCategory) {
def twoYear = currentdate - 2.year
println("Minus 2year: " + twoYear)

 def twoYear1 = today1 - 2.year
 println("Minus 2year: " + twoYear1)

}

 def twoYear = currentdate - 2.year
 println("Minus 2year: " + twoYear)

above statement is still the displaying the current date but format is correct
output in console: Minus 2year: 09/03/2019

 def twoYear1 = today1 - 2.year
 println("Minus 2year: " + twoYear1)

This statement is displaying the correct date but not sure how to apply the format MM/DD/YYYY
output in console: Minus 2year: Sun Sep 03 10:01:20 PDT 2017

please help me!!!

import groovy.time.TimeCategory

def today = new Date()
def currentdate = today

use(TimeCategory) {
    def twoYear = today - 2.year
    println twoYear.format("yyyy-MM-dd")
}

working as expected…Thanks alot Gontar for the immediate response.

even i am also doing the same but i have been stored the format in a variable and calling the same in the current date. any reason why it wasn’t worked with my code?

For variable twoYear1 was default format. You just nedd add format twoYear1.format(acceptedFormat)

import groovy.time.TimeCategory

def acceptedFormat = "yyyy-MM-dd"
def today1 = new Date()
def currentdate = today1.format(acceptedFormat)	


println(currentdate)

use(TimeCategory) {
	 def twoYear1 = today1 - 2.year
	 println("Minus 2year: " + twoYear1.format(acceptedFormat))
}

thanks a lot for the clarification :slight_smile: