[英]How to read date and time from an image file
如何从图像文件(已创建)中获取日期和时间作为yyyyy.MMMMM.dd GGG hh:mm aaa,这是我正在使用的代码。
Path p = Paths.get( "C:\\DowloadFolder\\2.png" );
BasicFileAttributes view = Files.getFileAttributeView( p, BasicFileAttributeView.class ).readAttributes();
System.out.println( view.creationTime()+" is the same as "+view.lastModifiedTime() );
我试图使用DateFormat dateFormat = new SimpleDateFormat( "dd/MM/yyyy" );
,但不知道如何从图像文件中获取日期和时间。
有一个名为metadata-extractor的库可以完成这项工作。
您似乎要求创建文件的日期时间。 但是你也可以在你的第一句话中引用一些日期格式。 然后你专门提到一个图像文件,暗示你想要嵌入jpeg和其他一些图像格式的元数据。
如果你想要的只是文件的创建时间,那么问题中的示例代码就已经有了这个信息,即java.nio.file.attribute.FileTime
对象。
这是我自己要演示的一些示例代码。
Path p = Paths.get( "/Volumes/Macintosh HD/Users/johndoe/text.txt" );
BasicFileAttributes view = null;
try {
view = Files.getFileAttributeView( p, BasicFileAttributeView.class ).readAttributes();
} catch ( IOException ex ) {
Logger.getLogger( App.class.getName() ).log( Level.SEVERE, null, ex );
}
// As of Java 7, the NIO package added yet another date-time class to the Java platform.
java.nio.file.attribute.FileTime fileTimeCreation = view.creationTime();
java.nio.file.attribute.FileTime fileTimeLastModified = view.lastModifiedTime();
fileTimeCreation
对象具有文件的创建日期时间信息。 阅读有关如何使用它的文档 。
要与其他类一起使用,您可能希望转换为另一种类型的日期时间对象。 在Java 8中使用Joda-Time或新的java.time。*包 。 避免使用Java捆绑的旧java.util.Date和Calendar类,因为它们非常麻烦。
别忘了时区。 通常最好指定所需的时区而不是依赖默认值。
// Convert to Joda-Time.
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
org.joda.time.DateTime dateTime = new DateTime( fileTimeCreation.toMillis(), timeZone );
// Convert to java.time.* package in Java 8.
ZoneId zoneId = ZoneId.of( "Europe/Paris" );
ZonedDateTime zonedDateTime = ZonedDateTime.parse( fileTimeCreation.toString() ).withZoneSameInstant( zoneId );
// Convert to java.util.Date
// Caution: I do not recommend using java.util.Date & Calendar classes. But if you insist…
java.util.Date date = new java.util.Date( fileTimeCreation.toMillis() );
转储到控制台......
System.out.println( "fileTimeCreation: " + fileTimeCreation );
System.out.println( "fileTimeLastModified: " + fileTimeLastModified );
System.out.println( "Joda-Time dateTime: " + dateTime );
System.out.println( "java.time zonedDateTime: " + zonedDateTime );
System.out.println( "java.util.Date (with default time zone applied): " + date );
跑的时候......
fileTimeCreation: 2014-02-16T02:28:51Z
fileTimeLastModified: 2014-02-16T02:34:17Z
Joda-Time dateTime: 2014-02-16T03:28:51.000+01:00
java.time zonedDateTime: 2014-02-16T03:28:51+01:00[Europe/Paris]
java.util.Date (with default time zone applied): Sat Feb 15 18:28:51 PST 2014
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.