java 获取文件夹中最大日期的文件夹

例如:如图所示

/**
 * 获取文件夹的最大日期
 * @param obj 文件夹路径传入图中G:\group2\group3
 * @return 文件夹中最大日期 返回图中最大值2020011120
 */
private Date getFolderLastDate(Object obj){
    LogTextUtils logTextUtils = LogTextUtils.getLogger();
    File directory = null;
    if (obj instanceof File) {
        directory = (File) obj;
    } else {
        directory = new File(obj.toString());
    }
    ArrayList<String> folderLastDate = new ArrayList<String>();
    if (directory.isDirectory()) {
        File[] fileArr = directory.listFiles();
        for (int i = 0; i < fileArr.length; i++) {
            File fileItem = fileArr[i];
            //是目录而且是以20结尾的目录
            if(fileItem.isDirectory() && (fileItem.getName().endsWith("20"))
            ) {
                folderLastDate.add(fileItem.getName());
            }
        }
    }
    Date lastDate=new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
    if(null!=folderLastDate && folderLastDate.size()==0){
        logTextUtils.wirteInfo("没有找到符合要求的文件夹,文件夹格式为:年月日20 例如:2020011220");
    }else {
        long temp = 0;
        for (int i = 0; i < folderLastDate.size(); i++) {
            String path = folderLastDate.get(i);
            long pathValue;
            try {
                pathValue = Long.parseLong(path);
                if (temp <= pathValue) {
                    temp = pathValue;
                }
            } catch (Exception e) {
                logTextUtils.wirteInfo("请按照规则放置目录,文件夹格式为:年月日20 例如:2020011220");
            }
        }
        logTextUtils.wirteInfo("最大的日期文件目录为:"+temp);
        try {
            lastDate = sdf.parse(String.valueOf(temp));
        } catch (ParseException e) {
            logTextUtils.wirteInfo("文件夹日期字符串转日期转换错误");
            e.printStackTrace();
        }
    }
    LocalDate lastLocalDate = lastDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    logTextUtils.wirteInfo("从文件夹中提取最大的日期文件夹为:"+lastLocalDate);
    return lastDate;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

softwareDragon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值