0

View:

<p:calendar id="dateRequisition" widgetVar="calwidget" pattern="yyyy-MM-dd HH:mm" value="#{examrequisitionsController.selected.dateRequisition}"
                                title="#{bundle.CreateExamrequisitionsTitle_dateRequisition}" required="true" requiredMessage="#{bundle.CreateExamrequisitionsRequiredMessage_dateRequisition}"
                                converterMessage="Invalid Birth Date." effect="slide" showOn="both" navigator="true" yearRange=":+1"
                                minHour="06" maxHour="18" stepMinute="10" disabledWeekends="true" readonlyInput="true">
    <f:validator validatorId="dateValidator" />
</p:calendar>

Validator:

public void validate(FacesContext context, UIComponent component,
        Object value) throws ValidatorException {
    String msg = "";
    if (value == null) {
        msg = "The Date field is required";
    } else {
        Date dateRequisition = null;
        try {
            dateRequisition = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ENGLISH).parse(value.toString());

        } catch (ParseException ex) {
            Logger.getLogger(DateValidator.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (!Persistence.createEntityManagerFactory("SystemCardiologyReportsPU").createEntityManager().createNamedQuery("Examrequisitions.findByDateRequisition")
                .setParameter("dateRequisition", dateRequisition).getResultList().isEmpty()) {
            msg = "The Date of requisition is already defined.";
        }
    }
    if (!"".equals(msg)) {
        JsfUtil.validatorMessage(msg);
    }
}

Column in DB: dateRequisition datetime NOT NULL.

I debugged and the value, which is received in the validator class is in the following format: (java.util.Date) Sun Nov 15 06:00:00 GMT-03:00 2015.

So, I want to format it to yyyy-MM-dd HH:mm and compare with all dates in my DB to test if it already contains in my DB.

Btw, it is always catching ParseException.

developer033
  • 24,267
  • 8
  • 82
  • 108

1 Answers1

0

I found the solution Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy"

dateRequisition = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH).parse(value.toString());

Community
  • 1
  • 1
developer033
  • 24,267
  • 8
  • 82
  • 108