pseudo
pseudo

Reputation: 848

g:formatDate alternative in a service

Can I somehow format a date in a way similar to how g:formatDate tag is doing this, but in Service? Including i18n and l10n?

It may not be the best practice to do this in a Service, as this should be more likely part of the view logic, but I need to return a date as a part of the JSON response and I don't want to deal with this in JS (mainly because of l10n).

Thanks for advice.

Upvotes: 4

Views: 3205

Answers (2)

Hassan ALAMI
Hassan ALAMI

Reputation: 347

You can use formatDate in a service or controller like that:

g.formatDate( date: new Date(), type: "date", style:"MEDIUM" )

EDIT: Doesn't work in services

Upvotes: 1

chim
chim

Reputation: 8573

You've got a few options here, that don't involve pulling in the tagLib bean from the grails application.

Option 1: If you're not too worried about i18n Format the date in the service using default groovy methods.

static final String DATE_FORMAT = 'dd-MM-yyyy'
// ...
Date date = new Date()
String formattedDate = date.format(DATE_FORMAT)

(That's all you need, no extra imports)

Option 2: If you are worried about i18n and still want to format the date in the service, then you can get the default date format

But first you need to get the Locale in the service, (this is where alarm bells should start ringing)

// Don't do this...
import org.springframework.context.i18n.LocaleContextHolder;
// ...
String locale = LocaleContextHolder.getLocale()
// Don't do that ^^^

This gets the Locale associated with the current thread, if any, or the system default Locale else. But we want the user's locale from the request or the session.

(Those alarm bells should be joined by red flashing lights now)

But if you do want to do this, then pass the request in from the controller via the method call.

Then you can use

import org.springframework.web.servlet.support.RequestContextUtils
// ...
def locale = RequestContextUtils.getLocale(request)

So we have the locale, and now we can get the message

// include the grailsApplication bean in the service
def grailsApplication
// ...

// Get the date format
def dateFormat = grailsApplication.mainContext.getMessage('default.date.format', null, 'dd-MM-yyyy', locale)

// Then you can use the groovy default method
String formattedDate = date.format(dateFormat)

Don't use a static method to get the request in the service. If you do this, the alarm bells and flashing red lights will be joined by a violent shaking of the building you're in. Just pass it in from the controller.

Option 3: Format the date in the view

<g:set var="formattedDate" value="${g.formatDate(date: date)}" />

This is undoubtedly the cleanest method, but sometimes you're not using templates to return the output.

Option 4:

Use a tagLib and output from the controller after getting the data in the service. e.g.

def model = service.getModel()
def output = myTagLib.myOutputHtmlMethod(model)

Then you can use g.formatDate in myTagLib

Upvotes: 0

Related Questions