Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/Groovy - simple date reformatting

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?

like image 216
DC Guy Avatar asked Jan 17 '13 16:01

DC Guy


People also ask

How do I change the date format in groovy?

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.


1 Answers

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 
like image 86
tim_yates Avatar answered Sep 19 '22 03:09

tim_yates