sayit sayit - 1 month ago 18
Java Question

Sorting dates with time

I am trying to sort dates with the times inside of my array strings. The dates sort just fine but I need to sort the times with the respective date. SORT TIMES NOT DATES. My column class is set up date. My date123 array prints out:

Here is the problem:

Variables:
Date[] date123 = new Date[8];
String rowdata[] = new String[8];


String array1[] = { dateString1(current time), "10:18:17 PM"};
String array2[] = {"1999/01/19 08:09:10 AM", "10:00:33 AM"};
String array3[] = {"1989/12/05 09:00:56 PM", "07:12:19 AM"};
String array4[] = { dateString1, "10:18:17 PM"};
String array5[] = {"2001/06/10 08:09:10 AM", "10:40:53 AM"};
String array6[] = {"2000/01/12 05:00:56 PM", "07:12:19 AM"};
String array7[] = { dateString1, "10:18:17 PM"};
String array8[] = {"2010/01/09 10:09:10 AM", "3:00:33 PM"};


rowdata[0] = array1[0];
rowdata[1] = array2[0];
rowdata[2] = array3[0];
rowdata[3] = array4[0];
rowdata[4] = array5[0];
rowdata[5] = array6[0];
rowdata[6] = array7[0];
rowdata[7] = array8[0];

for(int t = 0; t<rowdata.length; t++)
{
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a");

try {
date123[t] = sdf.parse(rowdata[t]);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return Boolean.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return Date.class;
default:
return Date.class;
}
}

data11.add(date123[t]);





Output:


Aug 21,2014

Jan 19, 1999

Dec 05, 1989

Aug 21, 2014

Jun 10,2001

Jan 12,2000

Aug 21,014

Jan 09,2010


But this is what is being put in table: just the date and not times. The dates are sorted, but I need the times to be printed with the dates, but I want the times to be sorted not DATES... Please help .

Answer

If you are using JTable to display your records and TableRowSorter to sort your rows, here you go.

JFrame frame = new JFrame("Sort Records based on Time");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Object rows[][] = { { "1999/01/19 08:09:10 AM", "10:00:33 AM" },
    { "2000/01/12 05:00:56 PM", "07:12:19 AM" },
    { "2010/01/09 10:09:10 AM", "3:00:33 PM" } };

String columns[] = { "Date", "Time" };

TableModel model = new DefaultTableModel(rows, columns) {
    private static final long serialVersionUID = 1L;

    public Class<? extends Object> getColumnClass(int column) {
    return getValueAt(0, column).getClass();
    }
};

JTable table = new JTable(model);

TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
sorter.setComparator(0, new Comparator<String>() {
    public int compare(String record1, String record2) {
    if (record1 != null && record2 != null) {
        final DateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
        try {
        Calendar time1 = Calendar.getInstance();
        Calendar time2 = Calendar.getInstance();
        time1.setTime(sdf.parse(record1.substring(record1.indexOf(" "))));
        time1.setTimeZone(TimeZone.getTimeZone("MDT"));
        time2.setTime(sdf.parse(record2.substring(record2.indexOf(" "))));
        time2.setTimeZone(TimeZone.getTimeZone("MDT"));

        if (time1.before(time2)) {
            return -1;
        } else if (time1.after(time2)) {
            return 1;
        }
        } catch (ParseException e) {
        // Do Nothing
        }
    }
    return 0;
    }
});

table.setRowSorter(sorter);

JScrollPane pane = new JScrollPane(table);

frame.add(pane, BorderLayout.CENTER);

frame.setSize(300, 150);
frame.setVisible(true);
Comments