-2

I have this code. Datum in games class is DateFormat, and it need to be DateFormat, but in sql I need set Date. How can I convert this DateFormat in Date in this line of code?

    try{

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost/internacionalizacija?useUnicode=true&characterEncoding=UTF-8", "root", "");

        String sql = "INSERT INTO utakmice(tim1, tim2, datum) VALUES(?,?,?)";
        PreparedStatement stmt = conn.prepareStatement(sql);
        //stmt.execute("set names 'utf8'");

        stmt.setString(1, utakmice.getTim1());
        stmt.setString(2, utakmice.getTim2());
        stmt.setDate(3, utakmice.getDatum());

Games class:

public class Games {
    private String tim1;
    private String tim2;
    private DateFormat datum;

    public Utakmice(String tim1, String tim2, DateFormat datum) {
        this.tim1 = tim1;
        this.tim2 = tim2;
        this.datum = datum;
    }


    public void setTim1(String tim1) {
        this.tim1 = tim1;
    }

    public String getTim1() {
        return tim1;
    }

    public void setTim2(String tim2) {
        this.tim2 = tim2;
    }

    public String getTim2() {
        return tim2;
    }

    public void setDatum(DateFormat datum) {
        this.datum = datum;
    }

    public DateFormat getDatum() {
        return datum;
    }
}

And i get date from JFrame, here is code:

private void btnSpremiActionPerformed(java.awt.event.ActionEvent evt) {                                          

    String tim1 = txtTim1.getText();
    String tim2 = txtTim2.getText();
    DateFormat datum = txtDate.getDateFormat();

    Utakmice utakmice = controll.kreirajUtakmice(tim1, tim2, datum);
    controll.spremiUtakmice(utakmice);
}   
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 2
    You should try to show some input/output example. This is not clear like this. See [ask] to improve your question a bit. Note that [`DateFormat`](https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html) is just a pattern used to create a `String` from a `Date` or the opposite. This is not a `Date`, **it doesn't hold any time data**. – AxelH Sep 04 '17 at 10:27
  • Hint: An object of type `DateFormat` is needed to parse a string to a `Date`-object. So where is your formatted date-string? – Meno Hochschild Sep 04 '17 at 10:28
  • `stmt.setDate(3, new Date());` – Nadeen Udantha Sep 04 '17 at 10:31
  • @MenoHochschild can you put some example of that, how it need to be? – Katja Bilobrk Sep 04 '17 at 10:31
  • This is hard to understand. Give wider contest, f.e. declarations – Jacek Cz Sep 04 '17 at 10:32
  • @AxelH I add Games class. – Katja Bilobrk Sep 04 '17 at 10:49
  • @AxelH yes, its a DatePicker.. ok, can you give me some simple example how `DateFormat` create `Date` from a `String`. That would help me a lot. – Katja Bilobrk Sep 04 '17 at 10:57
  • 1
    Since this is a `DatePicker`, you just used the wrong getter to select the `Date` picked in that component, you get its formatter. Unfortunatly, I don't know what component you used (I don't think Swing provide one). Could you show the `import` line where this `DatePicker` class is imported ? (Or give us the framework used). _PS : I will add some example of `DateFormat` usage with the answer, but you can't use this without the `Date` information._ – AxelH Sep 04 '17 at 11:01
  • 1
    @AxelH You meen this: `txtDate = new datechooser.beans.DateChooserCombo();` ?! I use `DateChoser.jar` – Katja Bilobrk Sep 04 '17 at 11:14
  • @KatjaBilobrk yes, it is exactly what I needed, the jar used. I didn't found documentation but the code should be correct enough. See my answer. You should edit your question to add that information (since this is needed to get the problem ;) ). You can clean the question by removing the methods in the `Games` class (not needed here, replace those line with `//getter and setter`) – AxelH Sep 04 '17 at 11:24
  • @AxelH ok. I have code without currently errors, but here is new problem. When I run my app I got a message: `Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date` – Katja Bilobrk Sep 04 '17 at 11:44
  • 1
    @KatjaBilobrk my bad, I should have seend the `PreparedStatement` error sooner... this is a different problem. We usually use `java.util.Date` (well, used), but from SQL, there is a `java.sql.Date`, same "name" but not the same instance. You can see how to create a `java.sql.Date` from a `java.util.Date` on [that thread](https://stackoverflow.com/questions/18614836/using-setdate-in-preparedstatement). Since this is a different problem, I will let my answer untouched, this is to far from the originial question. – AxelH Sep 04 '17 at 11:55

1 Answers1

1

You are currently getting a DateFormat from a date picker in a JFrame. You are just using the wrong getter, you should find a getDate(), getSelectedDate(), ... that return a Date, LocalDate, ...

You are using DateChooserCombo. Didn't find a javadoc yet but the source code I found show that it provide

public Calendar getSelectedDate() { 
    return chooser.getSelectedDate(); 
} 

It returns a Calendar, you can get a Date from it using Calendar.getTime()

So instead of have a DateFormat datum, update it to Date datum and change in your JFrame into

Date datum = txtDate.getSelectedDate().getTime(); //Might be null if nothing selected !!

And the bean :

public class Games{
    private String tim1;
    private String tim2;
    private Date datum; //CHANGED HERE

    //getter and setter
}

FYI :

Currently, the DateFormat recovered is used to create or stringify a Date using Date.format(Date) and Date.parse(String).

Here is a simple example using a SimpleDateFormat (a subclass of DateFormat allowing to create your own pattern, see the doc for more information)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//Convert a String into a Date (matching the pattern above
Date d = sdf.parse("2017-09-20");
//Convert a Date into a String(matching the pattern above
String s = sdf.format(d);
AxelH
  • 14,325
  • 2
  • 25
  • 55