DatePicker (Java 8+) : entering localDate without separator

UI Components for JSF
Post Reply
ritonglue
Posts: 21
Joined: 04 Jun 2021, 16:52

30 Jun 2023, 17:36

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

jepsar
Posts: 166
Joined: 03 Sep 2014, 11:41
Location: NL / BE
Contact:

30 Jun 2023, 19:54

Did you try to create a converter?
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub: https://github.com/jepsar
Spotify: 90s Rave, Acid, Trance, House

NOTiFY
Posts: 393
Joined: 25 May 2016, 22:57

02 Jul 2023, 13:42

PF 13.0.0--Jakarta. Jakarta Faces 4.0.1/Kotlin Multiplatform 1.9.10
Mojarra 4.0.2, OmniFaces 4.2
WildFly 29.0.1.Final 'preview' Jakarta EE 10.0.0
JDK 20.0.2, Kotlin 1.9.10, Gradle 8.3 Groovy DSL, MongoDB 7.0.0
IntelliJ IDEA 2023.2.1, macOS Ventura 13.5.1

ritonglue
Posts: 21
Joined: 04 Jun 2021, 16:52

03 Jul 2023, 11:30

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.

ritonglue
Posts: 21
Joined: 04 Jun 2021, 16:52

03 Jul 2023, 13:11

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>

ritonglue
Posts: 21
Joined: 04 Jun 2021, 16:52

03 Jul 2023, 15:37

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 ?

jepsar
Posts: 166
Joined: 03 Sep 2014, 11:41
Location: NL / BE
Contact:

04 Jul 2023, 09:02

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
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub: https://github.com/jepsar
Spotify: 90s Rave, Acid, Trance, House

ritonglue
Posts: 21
Joined: 04 Jun 2021, 16:52

05 Jul 2023, 17:59

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.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 74 guests