question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calendar: LocalDateTime validation too lenient

See original GitHub issue

Validation of java.util.Date is more strict than the validation of LocalDateTime. Example see below. Before PrimeFaces 8 I was using a custom Converter which sets DateTimeFormatter to ResolverStyle.STRICT. This would lead to the same behavior for LocalDateTime as for java.util.Date. I think BaseCalendarRenderer:convertToJava8DateTimeAPI should be updated to use ResolverStyle.STRICT in the DateTimeFormatter as this is consistent to java.util.Date parsing. Currently, by default ResolverStyle.SMART is used. Another way would be to add a new attribute to p:calendar and companions to let the user select the ResolverStyle. What do you think?

Is there any chance to get this into 8.0.Final if I submit a PR? 😬

Just for reference: I was discussing this already in the forum: PrimeFaces 8.0.RC2, p calendar: ResolverStyle.STRICT.

1) Environment

  • PrimeFaces version: 8.0.RC2

2) Expected behavior

When parsing a java.util.Date I get an validation error, if I enter 30.02.2020 00:00:00: date The same error should popup for LocalDateTime.

3) Actual behavior

The same date when validated to a LocalDateTime gets parsed but gets quietly adjusted: localdatetime A date of 32.02.2020 00:00:00 will popup the validation message 😩

4) Steps to reproduce

Enter the date: 30.02.2020 00:00:00 Example here: https://github.com/kaiwinter/primefaces-test

5) Sample XHTML

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>PrimeFaces Test</title>
    </h:head>
    <h:body>

         <h:form id="form">
             <p:growl id="msgs" showDetail="true" skipDetailIfEqualsSummary="true" />
              
             <h:panelGrid columns="4">
                 <p:outputLabel for="button1" value="Date:" />
                 <p:calendar
                        id="button1"
                        value="#{testView.date}"
                        showOn="button"
                        pattern="dd.MM.yyyy HH:mm:ss"
                        navigator="true"/>
                 <p:outputLabel for="button1Result" value="Parsed:" />
                 <p:outputLabel id="button1Result" value="#{testView.date}" />
                 
                 <p:outputLabel for="button2" value="LocalDateTime:" />
                 <p:calendar
                        id="button2"
                        value="#{testView.localDateTime}"
                        showOn="button"
                        pattern="dd.MM.yyyy HH:mm:ss"
                        navigator="true"/>
                 <p:outputLabel for="button2Result" value="Parsed:" />
                 <p:outputLabel id="button2Result" value="#{testView.localDateTime}" />
                  
             </h:panelGrid>
              
             <p:commandButton value="Submit" update="msgs button1Result button2Result" />
              
         </h:form>

    </h:body>
</html>

6) Sample bean

@Named
@ViewScoped
public class TestView implements Serializable {

   private Date date;
   private LocalDateTime localDateTime;

   public Date getDate() {
      return date;
   }

   public void setDate(Date date) {
      this.date = date;
   }

   public LocalDateTime getLocalDateTime() {
      return localDateTime;
   }

   public void setLocalDateTime(LocalDateTime localDateTime) {
      this.localDateTime = localDateTime;
   }

}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
christophs78commented, Jan 23, 2020

We should make this configureable via a new ResolverStyle-attribute. (IMO it depends on the usecase what´s “right”.) And we should do this for Calendar and DatePicker. PR is welcome. (Please also add a few UnitTest´s for this.)

0reactions
kaiwintercommented, Jan 24, 2020

Great, I’ve opened a new PR. Sorry for the inconvenience and thanks for your fast replies that makes it a pleasure to contribute here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to sanity check a date in Java - validation - Stack Overflow
The current way is to use the calendar class. It has the setLenient method that will validate the date and throw and exception...
Read more >
Java 8 Date - LocalDate, LocalDateTime, Instant - DigitalOcean
All the Date classes are mutable, so they are not thread-safe. It's one of the biggest problems with Java Date and Calendar classes....
Read more >
IsoChronology (Java SE 9 & JDK 9 ) - Oracle Help Center
This chronology defines the rules of the ISO calendar system. This calendar system is ... Obtains an ISO local date-time from another date-time...
Read more >
PrimeFaces 8.0.RC2, p:calendar: ResolverStyle.STRICT
I'm migrating from Primefaces 7 to 8. In the past I was using a custom converter so my model uses already LocalDateTime.
Read more >
Struct LocalDate - Noda Time
The parameters do not form a valid date. ArgumentNullException, calendar is null. Properties. Calendar. Gets the calendar system associated with this local date ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found