隨着絲毫不差說我正在嘗試從HTML5的日期時間輸入一個日期時間和它加入到我的數據庫是代碼將字符串轉換日期時間爲喬達日期時間,並將其添加到數據庫中
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;
}
似乎一切都很好沒有檢測到錯誤,但數據庫保持不變沒有添加原料。 有什麼不對? :/ 另外一個問題或一個問題: 什麼是我應該輸入使用模式,以便用戶應該輸入只有這種日期:
yyyy-mm-dd HH:mm:ss
PS:
當我試圖這樣做:
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;
}
它被添加到數據庫中,但日期值是空
爲用戶提供良好的輸入日期界面,請使用https://eonasdan.github.io/bootstrap-datetimepicker/輸入日期和時間的更好方法。如果你只想要日期,那麼使用https://bootstrap-datepicker.readthedocs.io/en/latest/。然後在後端參考 - https://stackoverflow.com/questions/3386520/parse-date-from-string-in-this-format-dd-mm-yyyy-to-dd-mm-yyyy – DASH