Ask a Question

Groovy How to change DateTime as dd-MM-yyyy

SOLVED
AAB
Regular Contributor

Groovy How to change DateTime as dd-MM-yyyy

Hello,

 

I have to compare 2 different files that I'm doing with a Groovy script.

For the first file, the date is as dd-MM-yyyy. The second file is in yyyy-MM-dd.

How can I change the second date in the format of the first one please?

The code where this should be emplemented (2nd file) is as thus:

//------------Concepts - Validity - Dates
def getDates(it, dateToSearch) {
  // Find date in all validity nodes for this specific concept
  def dateNode = it.validity.find{it.startDate == dateToSearch}

  // Set dummy value to empty string
    def date = ""

  // If we found the date, get the value!
  if (dateNode != null) { date = dateNode.value }
  return date
}

I'm writing this to a textfile as such:

parsedJson.concepts.each {
	dynamic = ""
	addToCsvLine(it.keyValue);
	//log the startDate
	addToCsvLine(it.validity.startDate)
	// Remove last char ","
	dynamic = dynamic.substring(0, dynamic.length() - 1);
	
	//insert a new line
  	dynamic += '\n'
  	//write everything to the textfile as UTF-8
  	outputFile.append(dynamic, 'utf-8')
}
def addToCsvLine(element) {
dynamic += "\"" + element + "\","

Has someone an idea please?

Thanks in advance,

Kind regards,

AboveAndBeyond

1 ACCEPTED SOLUTION

Accepted Solutions
AAB
Regular Contributor

Re: Groovy How to change DateTime as dd-MM-yyyy

@nmrao  thanks for your response.

I did it this way:

 

//------------Concepts - Validity - Dates
def convertDate(date) {
  SimpleDateFormat formatFromApi = new SimpleDateFormat("yyyy-MM-dd")
  Date parsedDate = formatFromApi.parse(date)

  SimpleDateFormat formatToGolden = new SimpleDateFormat("dd/MM/yyyy")
  String goldenDate = formatToGolden.format(parsedDate)
  return goldenDate
}

Kind regards,

AboveAndBeyond

View solution in original post

2 REPLIES 2
nmrao
Community Hero

Re: Groovy How to change DateTime as dd-MM-yyyy

Before comparing the dates, parse the string using respective format and convert it to Date object.

For eg:

def date = new Date().parse(format, stringDate)


Regards,
Rao.
AAB
Regular Contributor

Re: Groovy How to change DateTime as dd-MM-yyyy

@nmrao  thanks for your response.

I did it this way:

 

//------------Concepts - Validity - Dates
def convertDate(date) {
  SimpleDateFormat formatFromApi = new SimpleDateFormat("yyyy-MM-dd")
  Date parsedDate = formatFromApi.parse(date)

  SimpleDateFormat formatToGolden = new SimpleDateFormat("dd/MM/yyyy")
  String goldenDate = formatToGolden.format(parsedDate)
  return goldenDate
}

Kind regards,

AboveAndBeyond

View solution in original post

cancel
Showing results for 
Search instead for 
Did you mean: