Calendar support for LocalDate and LocalDateTime

UI Components for JSF
vanuatoo
Posts: 32
Joined: 22 Jan 2012, 12:17

18 Mar 2018, 09:59

Hello,

I use PrimeFaces 6.2 with JSF 2.3 and found inconvenient that p:calendar uses java.util.Date as a binding value.
I think it would be better to support LocalDate and LocalDateTime instead.
NetBeans 11.2, Payara 5.193.1, PrimeFaces 6.2.27, Mojarra 2.3.9

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

18 Mar 2018, 19:07

PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

19 Mar 2018, 00:08

You can easily add a converter. We will take care of it, to make it usable without converter, when PF swichtes to java8.
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

LarsD
Posts: 69
Joined: 25 Nov 2011, 22:47
Location: Dresden, Germany

19 Mar 2018, 15:01

Converter code

html:

Code: Select all

<p:calendar
	converter="#{....calenderDateTimeConverter}"
	converterMessage="..."
 />
java:

Code: Select all

public Converter getCalenderDateTimeConverter() {
		return new Converter() {

			@Override
			public Object getAsObject(
					FacesContext context,
					UIComponent component,
					String value) {
				return value;
			}

			@Override
			public String getAsString(
					FacesContext context,
					UIComponent component,
					Object value) {
				return value.toString();
			}
		};
	}
For LocalDateTime the ToString-method is defined like this:
"Outputs this date-time as a String, such as 2007-12-03T10:15:30."
See https://docs.oracle.com/javase/8/docs/a ... eTime.html

kind regards
Lars

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

19 Mar 2018, 15:27

You can also add it globally with @FacesConvert and setting the type.
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

20 Mar 2018, 10:48

tandraschko wrote:
19 Mar 2018, 15:27
You can also add it globally with @FacesConvert and setting the type.
+1

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

20 Mar 2018, 10:56

Thats my converter ;)

Code: Select all

@Named
@ApplicationScoped
@FacesConverter(forClass = LocalDate.class)
public class LocalDateTimeConverter implements Converter
{
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(extractPattern(component, context));
        try
        {
            return LocalDate.parse(value, formatter);
        }
        catch (Exception e)
        {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value)
    {
        if (value == null || (value instanceof String && StringUtils.isBlank((String) value)))
        {
            return "";
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(extractPattern(component, context));
        return formatter.format((LocalDate) value);
    }

    private String extractPattern(UIComponent component, FacesContext context)
    {
        // try to get infos from calendar component
        if (component instanceof Calendar)
        {
            Calendar calendarComponent = (Calendar) component;
            return calendarComponent.getPattern();
        }

        return null;
    }
}
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

20 Mar 2018, 12:19


vished
Posts: 479
Joined: 02 Feb 2014, 17:38

09 Apr 2019, 19:21

Did you implement this already in PF 7 ?
Or do we have to create a converter still on our own?
PF 8.0

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

09 Apr 2019, 19:22

Its slated for 7.1 and no one has fixed it yet: https://github.com/primefaces/primefaces/issues/4646
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 39 guests