1

As the tittle say I'm trying to retrieve a Date Time from HTML5 date time input and add it to my database here's the code

private void HandleDate(String date, Test test) {
   DateTime dt = null;
   try {
       dt = CheckDateValidity(date);
   } catch (TestManagementException e) {
       setErrors(FIELD_DATE, e.getMessage());
   }
   test.setDate(dt);

}

private DateTime CheckDateValidity(String date) throws TestManagementException {
    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-mm-dd hh:mm:ss");
    DateTime dateTime;
    try {
        dateTime = formatter.parseDateTime(date);
    } catch (Exception e) {
        throw new TestManagementException("date couldn't be converted");
    }
    return dateTime;
}

everything seems good no errors detected but the database stayed the same no raw was added. what's wrong ? :/ another problem or a question: what's the pattern that i should use in the input so that the user should enter only this kind of date:

yyyy-mm-dd HH:mm:ss

ps:

when i tried doing this:

 private void HandleDate(String date, Test test) {
    DateTime dt = null;
   try {
       CheckDateValidity(date, dt);
   } catch (TestManagementException e) {
       setErrors(FIELD_DATE, e.getMessage());
   }
   test.setDate(dt);

}

private void CheckDateValidity(String date, DateTime dateTime) throws TestManagementException {
    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-mm-dd hh:mm:ss");
   // DateTime dateTime;
    try {
        dateTime = formatter.parseDateTime(date);
    } catch (Exception e) {
        throw new TestManagementException("date couldn't be converted");
    }
   // return dateTime;
}

it was added to the database but the date value was empty

  • for user provide good interface to enter date, use https://eonasdan.github.io/bootstrap-datetimepicker/ a better way to enter date and time both. If you only want date then use https://bootstrap-datepicker.readthedocs.io/en/latest/ . Then on backend refer - https://stackoverflow.com/questions/3386520/parse-date-from-string-in-this-format-dd-mm-yyyy-to-dd-mm-yyyy – DASH Jun 03 '17 at 23:56

1 Answers1

0

For those of you having the same problem, I just found the problem and the solution. The problem is that: when I converted the string to a date I got this form :

yyyy-mm-ddThh:mm:ss.sss+01:00

while the database field is of this form :

yyyy-mm-dd hh:mm:ss

that's the reason why it couldn't be added to the database so the solution I found was this :

private void HandleDate(String date, Test test) {
    DateTime dt = null;
   try {
       dt = CheckDateValidity(date);
   } catch (TestManagementException e) {
       setErrors(FIELD_DATE, e.getMessage());
   }
   test.setDate(dt);
}


private DateTime CheckDateValidity(String date) throws TestManagementException {
    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    DateTime dateTime;

    try {
        dateTime = formatter.parseDateTime(date);
    } catch (Exception e) {
        throw new TestManagementException("Date couldn't be converted");
    }
    return dateTime;
}

the code did not change but I added this in the preparedStatement

        /****** connect to test table ******/
        Timestamp dt = new Timestamp(test.getDate().getMillis());
        preparedStatement = preparedStatementInitialisation(connection, SQL_INSERT,true,
                test.getTeacherId(),test.getModuleId(), dt, test.getType());
        int status = preparedStatement.executeUpdate();
        if (status == 0){
            throw new DAOException("Error while creating a user, 0 line added to the table!");
        }

so instead of coping directly the date from the test bean I converted it to a timeStamp and now it works perfectly

for the DateTime pattern in the html input I just made this one

<input type="datetime" name="date" id="date" placeholder="yyyy-mm-dd hh:mm:ss" pattern="[1-2]{1}[0-9]{3}-(0[0-9]{1}|1[0-2]{1})-([0-2]{1}[0-9]{1}|3[0-1]{1}) ([0-1]{1}[0-9]{1}|2[0-3]{1}):[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}">

and it works perfectly now the user must enter a date of a this type yyyy-MM-dd HH:mm:ss

I discovered another weird thing is that in formatter pattern when I used "yyyy-mm-dd hh:mm:ss" I had a problem in the month and hours when I converted the month it started from 01 and when I add a 00 as hours it didn't work but when I used yyyy-MM-dd HH:mm:ss it worked o.O weird heh