掲示板

Obtain current date format

7年前 に Julian Pfeil によって更新されました。

Obtain current date format

Junior Member 投稿: 72 参加年月日: 16/03/01 最新の投稿
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
7年前 に David H Nebinger によって更新されました。

RE: Obtain current date format (回答)

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
It is going to be based on the locale of the current user.
thumbnail
7年前 に Sagar A Vyas によって更新されました。

RE: Obtain current date format

Liferay Master 投稿: 679 参加年月日: 09/04/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
7年前 に David H Nebinger によって更新されました。

RE: Obtain current date format

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
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.
7年前 に Julian Pfeil によって更新されました。

RE: Obtain current date format (回答)

Junior Member 投稿: 72 参加年月日: 16/03/01 最新の投稿
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...