importjava.io.File;
importjava.io.InputStreamReader;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.StringTokenizer;
publicclassRectangleMethod{
publicstaticvoidmain(String[]args){
RectangleMethodrm=newRectangleMethod();
//Strings=rm.getFileCreateDate("D:/srdddd");
rm.getDate("D:/srdddd");
}
//方法一
publicStringgetDate(Stringpath){
Stringdate="";
SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");
Filef=newFile(path);
if(f.isDirectory()){
Filefiles[]=f.listFiles();
for(inti=0;i
}
}elseif(f.isFile()){
date=f.getName()+":"+df.format(newDate(f.lastModified()));
System.out.println(date);
}
returndate;
}
//方法二
publicStringgetFileCreateDate(Stringpath){
Stringdate="";
Filef=newFile(path);
if(f.isDirectory()){//判断为文件夹
Filefiles[]=f.listFiles();
for(inti=0;i
getFileCreateDate(files[i].getAbsolutePath());
}
}elseif(f.isFile()){//判断为文件
date=f.getName()+":"+getFileCreateDate(f);
//打印时间你也可以进行其他操作比如写入到一个文件中
System.out.println(date);
}
returndate;
}
publicStringgetFileCreateDate(File_file){
Filefile=_file;
try{
Processls_proc=Runtime.getRuntime().exec(
"cmd.exe/cdir"+file.getAbsolutePath()+"/tc");
BufferedReaderbr=newBufferedReader(newInputStreamReader(
ls_proc.getInputStream()));
for(inti=0;i<5;i++){
br.readLine();
}
Stringstuff=br.readLine();
StringTokenizerst=newStringTokenizer(stuff);
StringdateC=st.nextToken();
Stringtime=st.nextToken();
Stringdatetime=dateC.concat(""+time);
SimpleDateFormatformatter2=newSimpleDateFormat(
"yyyy-MM-ddhh:mm");
SimpleDateFormatformatter1=newSimpleDateFormat(
"yyyy/MM/ddHH:mm");
//System.out.println(formatter2.parse(datetime));
datetime=formatter1.format(formatter2.parse(datetime));
br.close();
returndatetime;
}catch(Exceptione){
e.printStackTrace();
returnnull;
}
}
}
代码如上,希望能帮到你