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 Eng开发者_高级运维lish? Any help? Thanks!
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')
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())
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}"/>
精彩评论