留言板

Obtain current date format

Julian Pfeil,修改在7 年前。

Obtain current date format

Junior Member 帖子: 72 加入日期: 16-3-1 最近的帖子
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,修改在7 年前。

RE: Obtain current date format (答复)

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
It is going to be based on the locale of the current user.
thumbnail
Sagar A Vyas,修改在7 年前。

RE: Obtain current date format

Liferay Master 帖子: 679 加入日期: 09-4-17 最近的帖子
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,修改在7 年前。

RE: Obtain current date format

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
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,修改在7 年前。

RE: Obtain current date format (答复)

Junior Member 帖子: 72 加入日期: 16-3-1 最近的帖子
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...