Search code examples
dategrailsgroovy

Groovy cannot parse String to Date


I want to parse String to Date using Groovy.

But I cannot parse to following java.lang.String to Date.

My code is like below.

import groovy.time.*

def date = new Date.parse("EEE MMM d HH:mm:ss z yyyy", "Fri Aug 28 11:12:11 +0000 2015")

But I get errors like below:

Unparseable date: "Fri Aug 28 11:12:11 +0000 2015". Stacktrace follows:
java.text.ParseException: Unparseable date: "Fri Aug 28 11:12:11 +0000 2015"
    at java.text.DateFormat.parse(DateFormat.java:366)

How can I parse it?


Solution

  • You need to set the appropriate Locale:

    Locale.setDefault(Locale.US)
    
    def date = Date.parse("EEE MMM dd HH:mm:ss Z yyyy", "Fri Aug 28 11:12:11 +0000 2015")
    

    Other mistakes are dd instead of d and Z instead of z.