Fork me on GitHub

Dates management

Lutèce provide a date utility class. DateUtil provide a list of functions lead to format, check and convert dates. This section will explain theses functions.

Section Display will explain how to add a Javascript calendar into html pages.

Format

In Lutèce, format is set for all dates in portal. It's a modification of SHORT style defined in class DateFormat :

  • For french language, expected format is DD/MM/YYYY ;
  • For english language, expected format is MM/DD/YYYY ;
  • For other languages, expected format corresponding to DateFormat.SHORT.

DateUtil provide a list of functions to format a date string to an appropriate format (Date, Timestamp, ...).

For each function, the Locale must be specified in parameters.

Validity

Check the validity of a date typed by the user is simply a call to DateUil.formatDate( ).

The function returns a type Date if the date typed is valid or null otherwise.

Conversion to string

To display a date, it's necessary to convert the date stored as a String. This conversion is done simply by using the following functions :

A function to retrieve directly the current date in String format :

The display format corresponds to that described in the Format section of this document.

Display

Display a date field in an html page can be decorated with a Javascript Calendar. Entering the date is being simplified.

The used Javascript calendar is the jquery UI Datepicker.

A set of Freemarker macros is available for calling the calendar. These macros are loaded systematically for each template (Front and Back office), it's not necessary to include them.

These macros and their descriptions are available in the file webapp/WEB-INF/templates/util/calendar/macro_datepicker.html.

Note : Datepicker macros can be called indifferently in the Front or Back Office templates.

Integration example

Examples of available macros :

  • <@getDatePicker /> : display for a single date field ;
  • <@getDatePickerRange /> : display for a time period (defined by two fields).

Here is an example of using a Datepicker macro :

<html>
	<title>
	...
	</title>
	<body>
		...
		<form ...>
			...
			<label class="text-label" for="my_date">Date : </label>
			<input type="text" size="10" id="my_date" name="my_date" value="${my_date?date}" />
			...
		</form>
		<@getDatePicker idField="my_date" language=locale.language />
		...
	</body>
</html>
					

The template is loaded in Freemarker with theses data :

  • my_date : variable (type java.util.Date). Freemarker Built-in ?date allow to display a date (year, month and day) ;
  • locale : the used locale (type Locale).