2017-06-03 45 views
1

隨着絲毫不差說我正在嘗試從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; 
} 

它被添加到數據庫中,但日期值是空

+0

爲用戶提供良好的輸入日期界面,請使用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

回答

0

對於那些你有同樣的問題,我只是發現問題和解決方案。 的問題是:當我轉換的字符串爲日期我有這種形式:

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

在數據庫領域是這樣的形式:

yyyy-mm-dd hh:mm:ss 

這就是爲什麼它不能被添加到數據庫中,所以我找到了解決辦法是這樣的:

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; 
} 

的代碼並沒有改變,但我說這在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!"); 
     } 

如此,而不是直接從測試豆應對之日起我將它轉換爲timeStamp,現在它完美

在HTML輸入DateTime模式我剛纔提出這個

<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}"> 

和它完美的作品現在用戶必須輸入這種類型的日期yyyy-MM-dd HH:mm:ss

我發現了另一個奇怪的是,在格式化模式,當我用"yyyy-mm-dd hh:mm:ss"我在這個月出現了問題,當時間我轉換了一個月從01開始,當我添加一個00作爲分,這沒有工作,但是當我用yyyy-MM-dd HH:mm:ss它的工作OO奇怪呵

相關問題