例如:如图所示 /** * 获取文件夹的最大日期 * @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; }
java 获取文件夹中最大日期的文件夹
最新推荐文章于 2024-06-24 10:55:58 发布