Java中文件创建时间和现在系统时间的时间间隔怎么计算
展开全部
package com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
/**
* Fileの时间を取得用クラス
*
* @author 徐小慧
* @version 1.0.0 $Id: Timetake 12254 2007-08-16 04:35:16Z
* xuxh $
* @since R1.0.0
*/
public class GetFileCreateTime {
/**
* Fileの时间
* @param _file _file
* @return datetime datetime
*/
public static String getFileCreateDate(File _file) {
File file = _file;
try {
Process ls_proc = Runtime.getRuntime().exec(
"cmd.exe /c dir " + file.getAbsolutePath() + " /tc");
BufferedReader br = new BufferedReader(new InputStreamReader(ls_proc.getInputStream()));
for (int i = 0; i < 5; i++) {
br.readLine();
}
String stuff = br.readLine();
StringTokenizer st = new StringTokenizer(stuff);
String dateC = st.nextToken();
String time = st.nextToken();
String datetime = dateC.concat(time);
br.close();
return datetime;
} catch (Exception e) {
return null;
}
}
public static int getDaysBetweenDate(Date d1, Date d2){
int days;
days = (int) ((d1.getTime() - d2.getTime())/(24*60*60*1000));
return days;
}
public static void main(String[] args){
//指定文件
File file = new File("D://安装工具//aalf.dat");
//获取创建日期
String time = GetFileCreateTime.getFileCreateDate(file);
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-ddhh:mm");
Date date = null;
try {
date = d.parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//打印时间间隔
System.out.println(getDaysBetweenDate(new Date(), date));
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
/**
* Fileの时间を取得用クラス
*
* @author 徐小慧
* @version 1.0.0 $Id: Timetake 12254 2007-08-16 04:35:16Z
* xuxh $
* @since R1.0.0
*/
public class GetFileCreateTime {
/**
* Fileの时间
* @param _file _file
* @return datetime datetime
*/
public static String getFileCreateDate(File _file) {
File file = _file;
try {
Process ls_proc = Runtime.getRuntime().exec(
"cmd.exe /c dir " + file.getAbsolutePath() + " /tc");
BufferedReader br = new BufferedReader(new InputStreamReader(ls_proc.getInputStream()));
for (int i = 0; i < 5; i++) {
br.readLine();
}
String stuff = br.readLine();
StringTokenizer st = new StringTokenizer(stuff);
String dateC = st.nextToken();
String time = st.nextToken();
String datetime = dateC.concat(time);
br.close();
return datetime;
} catch (Exception e) {
return null;
}
}
public static int getDaysBetweenDate(Date d1, Date d2){
int days;
days = (int) ((d1.getTime() - d2.getTime())/(24*60*60*1000));
return days;
}
public static void main(String[] args){
//指定文件
File file = new File("D://安装工具//aalf.dat");
//获取创建日期
String time = GetFileCreateTime.getFileCreateDate(file);
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-ddhh:mm");
Date date = null;
try {
date = d.parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//打印时间间隔
System.out.println(getDaysBetweenDate(new Date(), date));
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询