java获取最新时间的文件夹,java-如何从具有多个不同日期的文件的文件夹中获取特定日期的所有文件?...

Blockquote

I want to filter the files according to its date and time ,but i couldn't find any method in java .

please help if any one has any idea about it.`File fil =new File("C:\sujeet\efsfiles\iems\input");

FilenameFilter filter =new FilenameFilter(){

public boolean accept(File fil, String name){

return name.

}};

File[] temp= fil.listFiles(filter);

public void SeeFiles(){

for(File file : temp){

if(file.isFile()) {

count++;

System.out.println(file.getName());

}`

解决方案

The FileNameFilter here won't work as you are looking for the file attribute level

Check this can be any help

package com.tmp;

import java.io.IOException;

import java.nio.file.DirectoryStream;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.attribute.BasicFileAttributes;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

public class Tmp implements DirectoryStream.Filter {

public static void main( String[] args ) {

Path dir = Paths.get( "d:\\tmp" ); // Folder to search for files

try (DirectoryStream stream = Files.newDirectoryStream( dir, new Tmp() )) {

for ( Path entry : stream ) {

System.out.println( entry.getFileName() ); // file name which matched the accessed date

}

} catch ( IOException x ) {

System.err.println( x );

}

}

@Override

public boolean accept( Path file ) throws IOException {

try {

BasicFileAttributes attr = Files.readAttributes( file, BasicFileAttributes.class );

Date fileLastAccessedDate = new Date( attr.lastModifiedTime().toMillis() );

Calendar cal = Calendar.getInstance();

cal.setTime( fileLastAccessedDate );

cal.set( Calendar.HOUR_OF_DAY, 0 );

cal.set( Calendar.SECOND, 0 );

cal.set( Calendar.MINUTE, 0 );

cal.set( Calendar.MILLISECOND, 0 );

SimpleDateFormat sdf = new SimpleDateFormat( "MM-dd-yyyy" );

Date targetDate = sdf.parse( "2-20-2017" ); // date looking for the files

return ( cal.getTime().equals( targetDate ) );

} catch ( Exception x ) {

System.err.println( x );

return false;

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值