import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import edu.emory.mathcs.backport.java.util.Collections;
public class CollectionsSort {
private static final SimpleDateFormat FD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) throws ParseException {
final List<Date> stimes = new ArrayList<Date>();
Date stime1 = FD.parse("1997-10-29 00:00:00");
Date stime2 = FD.parse("1997-10-28 00:00:00");
Date stime3 = FD.parse("1997-10-30 00:00:00");
stimes.add(stime1);
stimes.add(stime2);
stimes.add(stime3);
System.out.println(stimes.toString());
Collections.sort(stimes);
System.out.println(stimes.toString());
String s = FD.format(stime3);
System.out.println(s);
}
}
输出:
[Wed Oct 29 00:00:00 CST 1997, Tue Oct 28 00:00:00 CST 1997, Thu Oct 30 00:00:00 CST 1997]
[Tue Oct 28 00:00:00 CST 1997, Wed Oct 29 00:00:00 CST 1997, Thu Oct 30 00:00:00 CST 1997]
1997-10-30 00:00:00
SimpleDateFormat.parse()方法,如果字符串格式不符合,会抛异常,感觉还是不太行。