Search code examples
javaswingjtablejdatechooser

Unexpected inclusion of data in a String where it shouldn't be


I am creating a Swing application using a JTable where you can enter a date and time into one of the columns. I am using JDateChooser and JComboBox drop downs which means I have to convert to String in quite a clumsy way:

Object[] row = new Object[2];

String Name = jTextField1.getText();

Object Hour = jComboBox1.getSelectedItem();
Object Minute = jComboBox2.getSelectedItem();
String Time1 = Hour.toString();
String Time2 = Minute.toString();

SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
String theTime = (Time1+ ":"+ Time2);
String theDate = dateFormat.format(jDateChooser1.getDate());
String together = (theDate + " " +theTime);

row[0] = Name;
row[1] = together;

model.addRow(row);

The issue is that if I input "21.02.2019 20:00", the output will be "21.02.2019 00:25 20:00", and 00:25 is my system time. I had a separate functionality that read the user's time but even when I disable it the problem persists. I see no way for the system time to be output here, so anyone's thoughts would be much appreciated.


Solution

  • The issue was with this line:

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
    

    This line meant that the value received from the JDateChooser was received in the format dd.MM.yyyy HH:mm, which I didn't realize. To fix the issue I simply removed 'HH:mm' from the above code.