Page 1 of 1

DatePicker (Java 8+) : entering localDate without separator

Posted: 30 Jun 2023, 17:36
by ritonglue
I would like to speed up localDate input and allow the user the enter a date without separators.

The facelet is :
<p:datePicker id="startDate" value="#{ordreComptablesView.startDate}" />
and startDate is a LocalDate.

Currently the user enters a date like this : 31/12/2023
It would be nice to be able to enter the date also like this : 31122023
At the end the display value is still 31/12/2023

Is it "simple" to do ? Could you make it available in the future ?

I use PF 12

Re: DatePicker (Java 8+) : entering localDate without separator

Posted: 30 Jun 2023, 19:54
by jepsar
Did you try to create a converter?

Re: DatePicker (Java 8+) : entering localDate without separator

Posted: 02 Jul 2023, 13:42
by NOTiFY

Re: DatePicker (Java 8+) : entering localDate without separator

Posted: 03 Jul 2023, 11:30
by ritonglue
I'm not sure if Omnifaces dates function can be useful.

A converter might be the solution. I tried to extend org.primefaces.convert.DateTimeConverter but for some reason it is not called.

As I have disabled the datePicker popup (showOnFocus = "false") I could use JSF "h:inputText" with "f:convertDateTime" but then the "look" of the component is not good.

Re: DatePicker (Java 8+) : entering localDate without separator

Posted: 03 Jul 2023, 13:11
by ritonglue
Here is my solution with converter. It would be great if such a nice feature appears in PrimeFaces :D

Code: Select all

<p:datePicker autocomplete="off" showOnFocus="false" id="dateGestion" value="#{cc.attrs.program.record.dateGestion}" >
	<f:convertDateTime type="localDate" dateStyle="short" />
	<f:ajax event="blur" render="@this" />
</p:datePicker>
The converter :

Code: Select all

public class DateTimeConverter extends org.primefaces.convert.DateTimeConverter {
	private static final Pattern PAT = Pattern.compile("[^a-zA-Z]");
	private static final Pattern PATD = Pattern.compile("\\d+");

	@Override
	public Object getAsObject(FacesContext context, DatePicker datePicker, String value) {
		try {
			String pattern = datePicker.calculatePattern();
			if(PATD.matcher(value).matches()) {
				String apattern = PAT.matcher(pattern).replaceAll("");
				var formatter = DateTimeFormatter.ofPattern(apattern);
				LocalDate date = LocalDate.parse(value, formatter);
				return date;
			}
		} catch(Exception e) {}
		return super.getAsObject(context, datePicker, value);
	}
}
In faces-config.xml :

Code: Select all

<converter>
	<converter-id>javax.faces.DateTime</converter-id>
	<converter-class>gbt.view.converter.DateTimeConverter</converter-class>
</converter>

Re: DatePicker (Java 8+) : entering localDate without separator

Posted: 03 Jul 2023, 15:37
by ritonglue
Now, how can I activate this converter everywhere ?
It looks like I have to go to every p:datePicker and add the f:convertDateTime.

Is it possible to enable my converter automatically ?

Re: DatePicker (Java 8+) : entering localDate without separator

Posted: 04 Jul 2023, 09:02
by jepsar
I always create custom tags for each input (sub) type. That way it's easy to do stuff like that globally.

In your case global attributes might help. https://primefaces.github.io/primefaces ... attributes

Re: DatePicker (Java 8+) : entering localDate without separator

Posted: 05 Jul 2023, 17:59
by ritonglue
That's right a custom tag might be interesting.

"Global Attributes" is a very interesting feature. Thanks for the tip.

@jepsar : you suggested somewhere else to use the mask in the datePicker. Thanks, it does what I want : fast input.

So I don't need my custom converter anymore.