0

Im doing a javafx program, which looks like this right now - I messed it up a bit cause I tried several ways, sorry about that:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     button2.setOnAction(e -> popupwindow.close());
    button1.setOnAction(e -> {
        
        String startDateString = startDateField.getText();
        String endDateString = endDateField.getText();
        Date startdate;
        Date enddate;
        try {
            startdate = sdf.parse(startDateString);
            enddate = sdf.parse(endDateString);
            System.out.println(startdate);
            System.out.println(enddate);
            Projects project =new Projects(Integer.parseInt(createdByField.getText()),sdf.parse(endDateString),countProjectIdsInList(),projectNameField.getText(),sdf.parse(startDateString));
            repo.saveProjects(project);
            projects.add(project);
            initProjectView();
            popupwindow.close();
        } catch (ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    
        });
         

But when I run the program, the following error occures:

com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect date value: 'Sat Nov 11 00:00:00 CET 1111' for column `projectplanner`.`project`.`end_date` at row 1

The concept is that I add the value of the dates in yyyy-MM-dd format into a textBox, then I want the program to convert the yyyy-MM-dd String into a Date value and upload it into the MySQL table, but in YYYY-MM-DD format aswell. If I understand it correctly, it means that I the MySQL cant convert the timeformat into the correct format. I use plain simple dates in the MySQL and Java too. But somehow the formatter doesnt even convert and I dont understand why. Can someone help please?

  • You are trying to save more data than it is excepted on your table attribute , change it's length so that it can hold larger values. – Dren Mar 03 '21 at 17:15
  • convert the "Sat Nov 11 00:00:00 CET 1111" into mysql date format. – glovemobile Mar 03 '21 at 19:45
  • But doesnt the simple date formatter form the input to yyyy-mm-dd when I pass the dates to it? –  Mar 03 '21 at 20:12
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 04 '21 at 18:14
  • Does this answer your question? [display Java.util.Date in a specific format](https://stackoverflow.com/questions/6262310/display-java-util-date-in-a-specific-format). Does [this](https://stackoverflow.com/questions/8745297/want-current-date-and-time-in-dd-mm-yyyy-hhmmss-ss-format)? The answer that you should really want is here: [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2). – Ole V.V. Mar 04 '21 at 18:15
  • [A Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) might help us help you. – Ole V.V. Mar 04 '21 at 18:16

0 Answers0