英文:
pick folders from windows path according to date name in java
问题
我需要选择最新的两个文件夹,根据它们的日期名称。JAVA
英文:
I have folders with names as yyyyMMdd format.
For example :
20200813
20200814
20200815
20210813
I need to select latest two folders according its date name. JAVA
答案1
得分: 1
一种可能的解决方案是获取文件列表,将它们按逆序排序,以使最新的文件排在前面,然后从列表中取前两个元素:
String directory = ...
List<File> fileList = Arrays.asList(new File(directory).listFiles());
fileList.sort(Comparator.reverseOrder());
File latestFile = fileList.get(0);
File secondLatestFile = fileList.get(1);
英文:
One possible solution is to get the file listing, sort them in reverse order so that the latest file comes first, and then take the first two elements from the list:
String directory = ...
List<File> fileList = Arrays.asList(new File(directory).listFiles());
fileList.sort(Comparator.reverseOrder());
File latestFile = fileList.get(0);
File secondLatestFile = fileList.get(1);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论