How to read date and time from image file

How to accurately get the date and time from the image file (which was created) in the form yyyyy.MMMMM.dd GGG hh: mm aaa, this is the code I'm using.

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() );

I tried to use DateFormat dateFormat = new SimpleDateFormat( "dd/MM/yyyy" );, but I don’t know how to get the date and time from the image file.

+4
source share
2 answers

Your question is confused

It seems you are requesting the date-time when the file was created. But you also refer to some date format in your first sentence. And then you refer specifically to the image file, which implies that you want the metadata to be embedded in jpeg and some other image formats.

, , , , 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 . , .

, . Joda-Time java.time. * Package in Java 8. java.util.Date Calendar, Java, , , .

. , .

// 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
+1
+1

Source: https://habr.com/ru/post/1527137/


All Articles