Fórum

Obtain current date format

Julian Pfeil, modificado 7 Anos atrás.

Obtain current date format

Junior Member Postagens: 72 Data de Entrada: 01/03/16 Postagens Recentes
LR7 GA3

I have a liferay-ui:date-input field in a form from which i grab the date on submission.
Because i save dates as strings and want to put them into Date objects i need to know the date format the current user uses.

E.g.: when i use the English interface the date format is MM/DD/YYYY and when i use the German the date format changes to DD/MM/YYYY.

How do i find the current date format?
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: Obtain current date format (Resposta)

Liferay Legend Postagens: 14919 Data de Entrada: 02/09/06 Postagens Recentes
It is going to be based on the locale of the current user.
thumbnail
Sagar A Vyas, modificado 7 Anos atrás.

RE: Obtain current date format

Liferay Master Postagens: 679 Data de Entrada: 17/04/09 Postagens Recentes
Hi Julian,

In ideal world of web , you should not accept any type date format.

I would always recommend to have consistency in date format either DD/MM/YYYY format or other way ,

you can do this by two way.

1. Put placeholder which will highlight format of date - so that user will know what format given form is expecting.
2. Give date picker option only - this is how you force user to choose date from picker and you can define a format in date picker.

Thanks,
Sagar Vyas
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: Obtain current date format

Liferay Legend Postagens: 14919 Data de Entrada: 02/09/06 Postagens Recentes
Sagar A Vyas:
In ideal world of web , you should not accept any type date format.


This is totally false.

In the ideal world of web, content is customized per the local standards of the user, not the whim of some company somewhere other than where you're at.

Your position starts with date formats but pretty soon you'll be saying number formats must match and everyone should surf the web in english. This is totally wrong.

All content should be tailored and customized for the user where they are at. Show content in their language, show numbers formatted the way they see them in their everyday world, same with date formats.
Julian Pfeil, modificado 7 Anos atrás.

RE: Obtain current date format (Resposta)

Junior Member Postagens: 72 Data de Entrada: 01/03/16 Postagens Recentes
My mistake was that i thought i had to know the date format to interpret liferay-ui:input-date's values.
I want to share two solutions i found, in case another newbie stumbles upon this:

1st Solution (and most simple one)
Just use the ...Param[/i] properties of input-date. E.g.:

JSP:
...
<liferay-ui:input-date name="..." dayParam="dopDayValueStart" monthParam="dopMonthValueStart" yearParam="dopYearValueStart" />


Controller:

...
    int startDay             = ParamUtil.getInteger(request, "dopDayValueStart");
    int startMonth           = ParamUtil.getInteger(request, "dopMonthValueStart");
    int startYear            = ParamUtil.getInteger(request, "dopYearValueStart");
    
    Calendar startDate = CalendarFactoryUtil.getCalendar(startYear, startMonth, startDay);


2nd Solution (getting the format)

public void test(ActionRequest request, ActionResponse response)
  {
    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
    User user = themeDisplay.getUser();
    Locale userLocale = user.getLocale();
    
    DateFormat fmt = DateFormat.getDateInstance(DateFormat.SHORT, userLocale);
    String pattern = ((SimpleDateFormat)fmt).toPattern();
    String localizedPattern = ((SimpleDateFormat)fmt).toLocalizedPattern();
    
    _log.info(pattern);
    _log.info(localizedPattern);
  // use pattern...
  }



I suggest using the first method whenever possible, because the second uses conversion-hackery...