Converts a 64-bit NTFS time value (number of 100-nanosecond intervals since January 1, 1601 UTC) to a Java Date . - Java java.util

Java examples for java.util:Date Parse

Description

Converts a 64-bit NTFS time value (number of 100-nanosecond intervals since January 1, 1601 UTC) to a Java Date .

Demo Code

/*//w w  w  .  ja v a 2s  .  c  o  m
 * $Id$
 *
 * Copyright (C) 2003-2014 JNode.org
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; If not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
//package com.java2s;
import java.util.Date;

public class Main {
    /**
     * Converts a 64-bit NTFS time value (number of 100-nanosecond intervals since January 1, 1601 UTC)
     * to a Java {@link Date}.
     *
     * @param filetime the FILETIME value.
     * @return the Java date.
     */
    public static Date filetimeToDate(long filetime) {
        return new Date(filetimeToMillis(filetime));
    }

    /**
     * Converts a 64-bit NTFS time value (number of 100-nanosecond intervals since January 1, 1601 UTC)
     * to a Java time value (number of milliseconds since January 1, 1970 UTC.)
     *
     * @param filetime the FILETIME value.
     * @return the number of milliseconds since 1970.
     */
    public static long filetimeToMillis(long filetime) {
        // Move the starting epoch to January 1, 1970.
        filetime -= 116444736000000000L;

        // Now convert the time into milliseconds, rather than 100-nanosecond units.
        if (filetime < 0) {
            filetime = -1 - ((-filetime - 1) / 10000);
        } else {
            filetime = filetime / 10000;
        }
        return filetime;
    }
}

Related Tutorials