2

want to convert date to the above required format. Is it possible to write a groovy script to do so in datamapper? If yes can you please give an example. Or can i reference another expression or component?

user3483129
  • 137
  • 6
  • 18

5 Answers5

4

I think the simplest way to do what you need is to edit the datamapper script and replace the line where you perform the date mapping for this one:

output.date = date2str(str2date(input.date,"d/mm/yy"), "yyyy-mm-dd");

but replacing the input and output fields accordingly.

M.T
  • 4,917
  • 4
  • 33
  • 52
MarcosNC
  • 356
  • 1
  • 6
0

Here is the link which explains how to do it in java. You may easily groovify it.

Here the script

import java.text.SimpleDateFormat

def OLD_FORMAT = "m/dd/yy"
def NEW_FORMAT = "yyyy-MM-dd"
// August 12, 2010
def oldDateString = "8/12/10"
def sdf = new SimpleDateFormat(OLD_FORMAT)
def d = sdf.parse(oldDateString)
sdf.applyPattern(NEW_FORMAT)
newDateString = sdf.format(d)
println "Date in modified format : $newDateString"

EDIT: Changed as per source format based on comment

Community
  • 1
  • 1
Rao
  • 20,781
  • 11
  • 57
  • 77
0

You can also use format method in the expression to set the date as below :

flowVars.Timestamp = server.dateTime.format("YYYYMMDDHHmm");
Rao
  • 20,781
  • 11
  • 57
  • 77
Krishna
  • 21
  • 1
0
#[server.dateTime.format('yyyy-MM-dd')]
chetan singhal
  • 948
  • 1
  • 13
  • 36
0

I'm not a fan of such nesting.

I prefer a variation on the above answers, where I only keep the inner function:

output.date = str2date(input.date,"m/dd/yy")

but then defined the output format in the datamapper's graphical editor, by setting the field to a date with format "yyyy-MM-dd".

Both of these approaches work, but I find this method easier to read and far easier to explain to others.

Adam Katz
  • 14,455
  • 5
  • 68
  • 83