I'm new to Java/Groovy development and I have a simple string that I would like to reformat, however I get an 'Unparseable date' error when I attempt to run the following:
import java.text.SimpleDateFormat import java.util.Date String oldDate Date date String newDate oldDate = '04-DEC-2012' date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldDate) newDate = new SimpleDateFormat("M-d-yyyy").format(date) println newDate
I'm sure it's something simple, but the solution eludes me. Can anyone help?
Date object and will have the Default formatting of 'E MMM dd HH:mm:ss z yyyy'. So the string date of '2012-12-11 00:00:00' will now appear as 'Tue Dec 11 00:00:00 EST 2012'. If you wish to change from the default date formatting you can append a . format() to function and pass it a string paramater.
With Groovy, you don't need the includes, and can just do:
String oldDate = '04-DEC-2012' Date date = Date.parse( 'dd-MMM-yyyy', oldDate ) String newDate = date.format( 'M-d-yyyy' ) println newDate
To print:
12-4-2012
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With