Results 1 to 5 of 5
  1. #1
    kogorman is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default Java last modified time

    Hi there,
    I have a question about the last modified time in java. I am returning the last modified time in two different ways, one is using the java.io.File and the second is using the new (i think its new) java.nio api.

    The problem is that when I call the lastmodified() and the lastmodifiedtime(), I get two different answers, but the answer for the time is always different by 1 hour.

    My modified 2007-22-06+15:57:36
    Modified attr 2007-06-22+14:57:36

    My modified 2007-26-07+15:16:26
    Modified attr 2007-07-26+14:16:26


    Am I doing something or do I just not understand the methods?
    Any ideas / help greatly appreciated.


    Java Code:
    public static void fileIterator() {
            Iterator it = FileUtils.iterateFiles(new File("/media/Vista"), null, true);
          
            int i = 0;
          
            File f = null;
            FileTime ctime = null;
            java.util.Date myDate = null;
            String DATE_FORMAT_NOW = "yyyy-dd-MM+HH:mm:ss";
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT_NOW);
    
            while (it.hasNext()) {
                i++;
          f = (File) it.next();
    
          BasicFileAttributes attrs = null;
                try {
                    attrs = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
                    ctime = (FileTime) Files.getAttribute(f.toPath(), "unix:ctime");
    
                } catch (IOException ex) {
                    Logger.getLogger(WindowsFileCall.class.getName()).log(Level.SEVERE, null, ex);
                }
                FileTime lastAccess = attrs.lastAccessTime();
    
                myDate = new java.util.Date(f.lastModified());
                sdf.format(myDate);
    
                System.out.println(f.getName());
                System.out.println("My modified " + sdf.format(myDate));
                System.out.println("Modified attr " + attrs.lastModifiedTime().toString().replace("T", "+").replace("Z", ""));
            
                System.out.println("Size " + org.apache.commons.io.FileUtils.sizeOf(f));
            }
        }

  2. #2
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    You do realise that ctime is the time of the last modification on the file's metadata?

    By the way, that is a weird time syntax.

    Edit:
    atime is the time of the last access
    mtime is the time of the last content modification
    ctime is the time of the last metadata modification
    creation time does not exist (by default)

    Edit 2:
    Looking at it [now] I can see why it is confusing.
    Last edited by Hibernate; 08-05-2011 at 03:57 AM. Reason: whoos, typo
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  3. #3
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Also have you check that method methods uses UTC (Zulu time)?
    You wouldn't happen to be in England (or so) using daylight saving time (as UTC is always in standard time)?
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  4. #4
    kogorman is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    Hi Hibernate,
    I'm not in the uk but close, I am in in Ireland so same time zone.

    I can see that f.lastModified() is returning GMT time but I'm not sure what attrs.lastModifiedTime() is returning, i couldn't see any thing in the Api about it. I am now looking attrs.lastModifiedTime().toString() to see what it is doing with the time.

  5. #5
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Please do not every use GMT as a [proper] time zone, since it is not well defined (but…, just use UTC [or UT, UT0, UT1, UT2, TT, AT, …], it is always better). In Java GMT = UTC, in real time keep, it is not!
    Referring to GMT does not tell if you are using standard time or daylight saving time.

    Would assume Files.getAttribute(f.toPath(), "unix:ctime" /*as well as unix:mtime and unix:atime*/) returns file system native time, which should be UTC+0 [with positive leap seconds as the previous non-leap second].

    It looks like f.lastModified() returns, not GMT [that is GMT as in UTC0], but [during the summer] BST (UTC0 + 1h advance due to daylight saving). And that is crappy, all time methods should return UTC and let you convert to civil time.
    It also looks like attrs.lastModifiedTime() returns UTC.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

Similar Threads

  1. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  2. Mark java.awt.Frame as modified
    By Patrick974 in forum AWT / Swing
    Replies: 0
    Last Post: 11-16-2010, 05:34 PM
  3. Replies: 1
    Last Post: 04-13-2010, 10:58 PM
  4. Modified Pixels
    By monkey04 in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 08:15 AM
  5. Replies: 0
    Last Post: 04-04-2008, 02:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •