0

I'm using primefaces and I have a datePicker <p:datePicker id="birthDate" value="#{accountBean.accountModel.birthDate}" pattern="yyyy-MM-dd"/> and I have an model like this :

public class AccountModel implements Serializable{

    public String accountId;
    public String name;
    public String address;
    public Date birthDate;
    public boolean allowNegativeBalance;
    public BigDecimal balance;
    
    // Getter Setter
}

The problem is when I try to System.out.println(accountModel.getBirthDate()); the result is Tue Feb 23 00:00:00 ICT 1997 (Date) . How to make the Tue Feb 23 00:00:00 ICT 1997 (Date) to 1997-02-23 (Date) when I access the getter of my model? Thank you.

muhkanda
  • 319
  • 1
  • 9
  • 2
    Use `LocalDate` whose `toString` returns the value in the form `yyyy-MM-dd`. – Arvind Kumar Avinash Apr 16 '21 at 06:08
  • 2
    I recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead as @ArvindKumarAvinash said use `LocalDate` [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 16 '21 at 06:11
  • If i use java.time.LocalDate, i got other error : javax.servlet.ServletException: WELD-000049: Unable to invoke public void com.daksa.mockvaJsf.bean.AccountBean.init() on com.daksa.mockvaJsf.bean.AccountBean@df6d586 Caused by: java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDate` (no Creators, like default construct, exist): no String-argument constructor/factory :) Thanks. – muhkanda Apr 16 '21 at 06:16
  • 1
    Ah, for use with Jackson you also need [jackson-modules-java8](https://github.com/FasterXML/jackson-modules-java8). Go ahead. – Ole V.V. Apr 16 '21 at 06:24
  • @OleV.V. Perfect! Thank you! – muhkanda Apr 16 '21 at 06:53

0 Answers0