我正在尝试使用数组字符串中的时间对日期进行排序。日期排序很好,但我需要用相应的日期对时间进行排序。排序时间不是日期。我的列类是设置日期。我的date123数组打印出来:
问题在于:
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]);
输出:
2014年8月21日
1999年1月19日
1989年12月5日
2014年8月21日
2001年6月10日
2000年1月12日
8月21,014
2010年1月9日
但这就是表格中的内容:只是日期而不是时间。日期已经过排序,但我需要打印日期的时间,但我希望时间排序不是日期...请帮忙。
答案 0 :(得分:0)
我假设您正在尝试根据时间字段对记录进行排序。每条记录都有日期和时间,但您希望根据时间进行排序。如果是这样,下面的代码将有所帮助。如果没有让我清楚地了解你的要求。
final DateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
String[][] records = new String[][] {
{ "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" } };
List<String[]> sortedList = new LinkedList<String[]>(Arrays.asList(records));
Collections.sort(sortedList, new Comparator<String[]>() {
public int compare(String[] record1, String[] record2) {
if (record1 != null && record2 != null
&& !(record1.length < 2 || record2.length < 2)) {
try {
Calendar time1 = Calendar.getInstance();
Calendar time2 = Calendar.getInstance();
time1.setTime(sdf.parse(record1[1]));
time1.setTimeZone(TimeZone.getTimeZone("MDT"));
time2.setTime(sdf.parse(record2[1]));
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;
};
});
for (String[] record : sortedList) {
System.out.println(record[0] + ", " + record[1]);
}
答案 1 :(得分:0)
如果您使用JTable显示记录,使用TableRowSorter对行进行排序,请转到此处。
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);