Groovy Date Parsing and Formatting

How to actually change the format of a string date in Groovy:

After many failed attempts to correctly change the format of a string date in groovy I finally figured out how to do this properly. This makes use of the java.util.date  package and is one of many extensions in groovy to java. This works because almost everything that works in java will work in groovy since groovy is extension of java. This particular Date.parse(String format, String input) acts as a wrapper for SimpleDateFormat. Date.parse('yyyy-MM-dd hh:MM:SS', '2012-12-11 00:00:00').format('E MMM dd HH:mm:ss z yyyy') So lets take a look at exactly how this is working (This is more an exercise in me remembering). Date.parse is a static method and can be directly called from anywhere without instantiating a date object. Parsing a string date is simple and works like this. Date.parse(String format, String dateInput) Pass the parse function the format of your input and your date as a string.

Note: This format does not change how your string date is output, it simply gives parse the correct context (format) of what you are attempting to parse with it. Date.parse('yyyy-MM-dd hh:mm:SS', '2012-12-11 00:00:00') This code given these two parameters will return a java.util.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. Date.parse(String inputFormat, String inputDate).format(String outputFormat)

Available formats for Dates:
In addition each symbol in the following table explains what it can be used for and youc an then use them to design your own custom format patterns for dates and times.

Collection of Resource links on Java and Groovy date functions:

Groovy Dates Documentation: extension of java.util.date

Java Dates Documentation:  java.util.date

Java SimpleDateFormat