步驟,你可以嘗試做...
- >如果你確信「\ n」中的每個條目可以說Ÿ則僅使用組織各項目獲取日期部分。 apache.commons.lang.StringUtils.substringBefore(字符串,字符串)說X
- >下一步是使用java.text.SimpleDateFormat中(YYYY-MM-DD)x到日期解析
- >然後可以使用任何集合對象可以包含鍵和值的對,其中關鍵的是x和y值(使用TreeMap的這可以自動排序的值。)
問題的這種方法:如果x部分在您的陣列的多個條目中很常見,則此方法不起作用。
試着運行這段代碼。最後的打印語句會給你排序的結果。地圖內的值是你正在尋找的。
class TestClass {
public void checkThis() throws ParseException {
String[] sampleValues = new String[3];
sampleValues[0]= "2013-7-13 \n 12 hour(s) 23 minute(s) ";
sampleValues[1]= "2013-8-14 \n 12 hour(s) 23 minute(s) ";
sampleValues[2]= "2013-5-16 \n 12 hour(s) 23 minute(s) ";
System.out.println("--> before"+ sampleValues);
String x= null;
Date date =null;
Map<Date, String> map = new TreeMap<Date, String>();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
for (String y : sampleValues) {
x = StringUtils.substringBefore(y, " \n");
date= formatter.parse(x);
map.put(date, y);
}
System.out.println(map.keySet());
}
}
使用自定義類!包含** **日期對象**和日期 –
的字符串表示形式的類是它的時間戳列表列表,還是具有時間戳記作爲其屬性之一的對象的列表列表。 –
千禧年比較對象一個接一個。 –