Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.util.Date comparison with LocalDate / LocalDateTime & friends #273

Open
pizzi80 opened this issue Nov 27, 2024 · 0 comments
Open

java.util.Date comparison with LocalDate / LocalDateTime & friends #273

pizzi80 opened this issue Nov 27, 2024 · 0 comments

Comments

@pizzi80
Copy link

pizzi80 commented Nov 27, 2024

Reading the 1.9 section of EL spec

https://jakarta.ee/specifications/expression-language/5.0/jakarta-expression-language-spec-5.0#relational-operators

there are some automatic conversion for BigDecimal, Double and Float
to correctly compare them, but this behaviour is not applied to
Date / LocalDate / LocalDateTime / ZonedDateTime

in EL 5.0 (Tomcat 10.1.33) the following expression throws an exception

#{ bean.date lt currentDate }
@Named @Produces @RequestScoped 
public java.util.Date getCurrentDate() { return new java.util.Date(); }
@Named
@RequestScoped
public class Bean {

  private LocalDate date = LocalDate.now().minusDays(1);
   
  public LocalDate getDate() {
    return date;
  }

}

The Exception:

java.lang.ClassCastException: class java.util.Date cannot be cast to class java.time.chrono.ChronoLocalDate (java.util.Date and java.time.chrono.ChronoLocalDate are in module java.base of loader 'bootstrap')
	at java.base/java.time.LocalDate.compareTo(LocalDate.java:139)
	at org.apache.el.lang.ELSupport.compare(ELSupport.java:126)
	at org.apache.el.parser.AstLessThan.getValue(AstLessThan.java:45)
	at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:152)
	at org.jboss.weld.module.web.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
	at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:73)

Expected Behavior

something like:

If A or B is LocalDate, coerce both A and B to LocalDate and use the return value of A.compareTo(B)

If A or B is LocalDateTime, coerce both A and B to LocalDateTime and use the return value of A.compareTo(B)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant