从Windows路径中按日期名称在Java中选择文件夹。

huangapple go评论71阅读模式
英文:

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&lt;File&gt; fileList = Arrays.asList(new File(directory).listFiles());

fileList.sort(Comparator.reverseOrder());

File latestFile = fileList.get(0);
File secondLatestFile = fileList.get(1);

huangapple
  • 本文由 发表于 2020年8月13日 19:54:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63394606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定