Java(자바)
Java로 File 만든 날짜 보기
마샤와 곰
2019. 4. 25. 14:00
자바에서 파일이 수정된 일자는 File 클래스를 통해서 쉽게 가져 올 수 있다.
그런데 파일이 만들어진 일자는, File 클래스에서 제공하지 않는다.
JDK 1.7부터 BasicFileAttributes라는 클래스를 제공하여주는데 해당클래스를 활용하면 쉽게 가져 올 수 있다.
물론 1.7부터 지원하니 그 이하 버전에서는 Class not found 될 것이다.
File file = new File("E:/test.txt");
BasicFileAttributes attrs = null;
try {
attrs = Files.readAttributes(file.toPath(),BasicFileAttributes.class);
String date = attrs.creationTime().toString();
System.out.println("Attr to String :" + date); //일반 데이터 형식 출력
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String convertDay = format.format(new Date(attrs.creationTime().toMillis()));
System.out.println("Attr to millis and convert to simpleDateFormat : "+convertDay); //simple데이터로 포맷한 형식
} catch (Exception e) {
e.printStackTrace();
}
반응형