Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy - Converting a date string to a formatted date

Tags:

date

groovy

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.

like image 497
Eric Snyder Avatar asked Jan 22 '14 20:01

Eric Snyder


People also ask

How do I change the date format in groovy?

String newDate = Date. parse('MM/dd/yyyy',dt). format("yyyy-MM-dd'T'HH:mm:ss'Z'");

Which function is used convert string into date format?

Date() Function. as. Date() function in R Language is used to convert a string into date format.

How do I print current date in groovy?

I had to add def or SimpleDateFormat before sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss") using Java 12 in a Gradle build file.


2 Answers

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.

like image 67
ataylor Avatar answered Oct 23 '22 01:10

ataylor


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
like image 29
ASHWATH RAJ Avatar answered Oct 23 '22 03:10

ASHWATH RAJ