0

Here's what I did:

java.util.Date now = new Date();
java.sql.Date date = new java.sql.Date(now.getTime());
String query;
String combo = combo_item.getSelectedItem().toString();

query = "INSERT INTO history(Date, ItemName)\n" +
                    "VALUES (?, '"+combo+"')";

                pst = con.prepareStatement(query);
                pst.setDate(1, date);
                pst.executeUpdate();

When I run it, the date format turns out to be default. Now, it tried to format it with this code:

     SimpleDateFormat sm = new SimpleDateFormat("MM/dd/yyyy HH:mm");
     String strDate = sm.format(myDate);
     Date dt = sm.parse(strDate);

but it never works. The date format was still default =(

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I recommend you don’t use `SimpleDateFormat` and the two `Date` classes. Those classes are poorly designed and long outdated, the first in particular notoriously troublesome. Instead use for example `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 27 '19 at 19:57
  • What is the datatype of the `Date` column in Access? Asking because I’m wondering whether it can hold a time of day. You should format your date-times for the UI only and let the database decide how it stores them. – Ole V.V. Feb 27 '19 at 20:00
  • I already use LocalDateTime and DateTimeFormatter but still not ff the format. – newbie_programmer Mar 01 '19 at 11:48
  • The data type was Date/Time. I also try the formatting in ms access and it formatted the date but when I retrieve it to display in jtable it turns back to default format. – newbie_programmer Mar 01 '19 at 11:50
  • Use formatting in your UI only. Since the UI is a `JTable`, I suggest using a custom renderer that format the date-time to a string in your desired format. Your question may possibly be a duplicate of [display Java.util.Date in a specific format](https://stackoverflow.com/questions/6262310/display-java-util-date-in-a-specific-format) or [SimpleDateFormatter.parse giving output in different format than specified](https://stackoverflow.com/questions/50494859/simpledateformatter-parse-giving-output-in-different-format-than-specified). – Ole V.V. Mar 01 '19 at 12:02

0 Answers0