- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
-
- public class ReadFileTime {
-
- public static void main(String[] args) {
- getCreateTime();
- getModifiedTime_1();
- getModifiedTime_2();
- }
-
-
- public static void getCreateTime(){
- String filePath = "C:\\test.txt";
- String strTime = null;
- try {
- Process p = Runtime.getRuntime().exec("cmd /C dir "
- + filePath
- + "/tc" );
- InputStream is = p.getInputStream();
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
- String line;
- while((line = br.readLine()) != null){
- if(line.endsWith(".txt")){
- strTime = line.substring(0,17);
- break;
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("创建时间 " + strTime);
-
- }
-
- @SuppressWarnings("deprecation")
- public static void getModifiedTime_1(){
- File f = new File("C:\\test.txt");
- Calendar cal = Calendar.getInstance();
- long time = f.lastModified();
- cal.setTimeInMillis(time);
-
- System.out.println("修改时间[1] " + cal.getTime().toLocaleString());
-
- }
-
-
- public static void getModifiedTime_2(){
- File f = new File("C:\\test.txt");
- Calendar cal = Calendar.getInstance();
- long time = f.lastModified();
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- cal.setTimeInMillis(time);
- System.out.println("修改时间[2] " + formatter.format(cal.getTime()));
-
- }
- }
posted @
2018-08-13 09:51
Graffiti
阅读(
31736)
评论()
编辑
收藏
举报