Eric Snyder
Eric Snyder

Reputation: 1924

Groovy - Converting a date string to a formatted date

OK, I am trying to convert a date string from a format like:

2014-01-21 00:00:00

to

01/21/2014

I have tried many variations and am crashing and burning. The issue is that to test I have to create the script, export it in a process in Bonita (a BPM software), Import it and then create some cases. This all takes a long time.

Hopefully someone knows how to do this.

Also, is there a simple groovy editor out there? That would help me learn how to write groovy very quickly.

Upvotes: 6

Views: 43630

Answers (2)

ASHWATH RAJ
ASHWATH RAJ

Reputation: 47

String olddate='2014/01/21 00:00:00'
Date date = Date.parse("yyyy/MM/dd HH:mm:ss",olddate)
String newDate = date.format( 'MM/dd/yyyy' )
log.info newDate

Upvotes: 2

ataylor
ataylor

Reputation: 66069

Groovy Dates have methods parse and format for converting to and from strings in various formats:

def format1 = '2014-01-21 00:00:00'
def format2 = Date.parse("yyyy-MM-dd hh:mm:ss", format1).format("dd/MM/yyyy")
assert format2 == '01/21/2014'

The format of the format strings are the same as Java's SimpleDateFormat.

Upvotes: 12

Related Questions