Bruno Morgado
Bruno Morgado

Reputation: 507

grails date format in English language

I have the following code to format the date:

def currentDate = new Date().format('E, dd MMM yyyy')

The format is as I expected, however it is written in the language of my computer. How can I get it to give the date in English? Any help? Thanks!

Upvotes: 0

Views: 12130

Answers (3)

Dónal
Dónal

Reputation: 187499

If you're doing this in a controller, taglib or GSP, try using the formatDate tag instead.

g.formatDate(date: new Date(), format: 'E, dd MMM yyyy', locale: Locale.ENGLISH)  

In a GSP you can also invoke it using this tag syntax:

<g:formatDate date="${new Date()}" format='E, dd MMM yyyy', locale="${Locale.ENGLISH}"/>

Upvotes: 0

sbglasius
sbglasius

Reputation: 3124

If you're running in context of a Controller I would suggest you use

def currentDate = new Date()

def formattedDate = g.formatDate(date:currentDate, format: 'E, dd MMM yyyy')

Upvotes: 6

Igor Artamonov
Igor Artamonov

Reputation: 35961

You can use standard date formatter:

import java.text.*
import java.util.Locale

DateFormat formatter = new SimpleDateFormat('E, dd MMM yyyy', Locale.US)
formatter.format(new Date())

Upvotes: 2

Related Questions