I keep getting this error
class java.time.format.Parsed cannot be cast to class java.time.LocalDate (java.time.format.Parsed and java.time.LocalDate are in module java.base of loader 'bootstrap')
I am trying to insert date and time values into my database.
My code:
String sTime = Helper.readString("Enter Closing Time (HH:MM:SS) > ");
try {
Time time = new Time(timeFormat.parse(sTime).getTime());
String sDate = Helper.readString("Enter Date (YYYY-MM-DD) > ");
LocalDate date = (LocalDate) dateFormat.parse(sDate);
String event = Helper.readString("Enter Event > ");
addStadium(id, name, category, time, date, event);
} catch (ParseException e) {
}
private void addStadium(String id, String name, String category, Time time, LocalDate date, String event) {
try {
String query = "INSERT INTO Stadium(ID, Name, Category, ClosingTime, DateUnavailable, Event) "
"SELECT J.ID,J.Name, J.Category, J.ClosingTime, U.DateUnavailable, U.Event FROM jogging_spot J INNER JOIN unavailability_date U ON J.ID = U.ID "
"VALUES('" id "', '" name "', '" category "', '" time ", " date ", '" event "')";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
CodePudding user response:
you can use LocalTime instead of Time
i modified your code in this way:
enter code here `private void addStadium(String id, String name, String category, LocalTime time, LocalDate date, String event) {
try {
String query = "INSERT INTO Stadium(ID, Name, Category, ClosingTime, DateUnavailable, Event) "
"SELECT J.ID,J.Name, J.Category, J.ClosingTime, U.DateUnavailable, U.Event FROM jogging_spot J INNER JOIN unavailability_date U ON J.ID = U.ID "
"VALUES('" id "', '" name "', '" category "', '" Time.valueOf(time) ", " Date.valueOf(date) ", '" event "')";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}`
i hope it will help you
CodePudding user response:
Try using
LocalDate.parse(dateString, formatter)
DateTimeFormatter format in "yyyy-MM-dd". Reference : https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html