日期:2014-05-20 浏览次数:20857 次
package david.deepInCollection;
import java.util.Calendar;
public class SearchedFile implements Comparable<SearchedFile> {
private String _fileName;
private Calendar _lastModifed;
public SearchedFile(String fileName, Calendar lastModified) {
// TODO Auto-generated constructor stub
_fileName = fileName;
_lastModifed = Calendar.getInstance();
_lastModifed.setTimeInMillis(lastModified.getTimeInMillis());
}
@Override
public String toString() {
// TODO Auto-generated method stub
return String.format("{文件名:%s, 最后修改时间:%s}", _fileName, _lastModifed
.getTime().toString());
}
@Override
public int compareTo(SearchedFile o) {
// TODO Auto-generated method stub
if (o == null)
return -1;
return _lastModifed.compareTo(o._lastModifed) > 0 ? -1 : 1;
}
}
public static void searchFilesDemo() {
Calendar calendar = Calendar.getInstance();
calendar.set(2013, 0, 1, 0, 0, 0);
String filepath = "D:\\";
File file = new File(filepath);
Calendar lastmodifiedDate = Calendar.getInstance();
List<SearchedFile> searchedResult = new ArrayList<SearchedFile>();
for (File item : file.listFiles()) {
lastmodifiedDate.clear();
lastmodifiedDate.setTimeInMillis(item.lastModified());
if (lastmodifiedDate.compareTo(calendar) > 0) {
if (!item.isHidden()) {
SearchedFile sf = new SearchedFile(item.getName(),
lastmodifiedDate);
searchedResult.add(sf);
}
}
}
Collections.sort(searchedResult);
for (SearchedFile searchItem : searchedResult) {
System.out.println(searchItem.toString());
}
}
public SearchedFile(String fileName, Calendar lastModified) {
// TODO Auto-generated constructor stub
_fileName = fileName;
// _lastModifed = Calendar.getInstance();
// _lastModifed.setTimeInMillis(lastModified.getTimeInMillis());
_lastModifed = lastModified;
}
第一种正常结果是:
{文件名:Java, 最后修改时间:Tue Oct 15 00:05:34 CST 2013}
{文件名:360安全浏览器下载, 最后修改时间:Tue Oct 15 00:05:03 CST 2013}
第二种不正常结果是:
{文件名:飞速Tudou, 最后修改时间:Sun Jan 27 16:27:34 CST 2013}
{文件名:灌篮高手全国大赛, 最后修改时间:Sun Jan 27 16:27:34 CST 2013}