filetime格式和datetime格式相互转换

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FiletimeToDateTime {
   
    public static void main(String[] args) throws ParseException {       
       
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       /**      将日期格式的转换成高低位 ,在此省略。读者自己修改 */
       // Date date1 = sdf.parse("2010-07-10 07:35:18");       
       //  FileTime fileTime = FileTime.date2FileTime(date1);
       // System.out.println(fileTime.toString());
 
        /**  end  */
       
        FileTime file = new FileTime();
        file.setLow(Long.valueOf(args[0]));
        file.setHigh(Long.valueOf(args[1]));
        Date date2 = FileTime.fileTime2Date(file);
        System.out.println(sdf.format(date2));
    }   
}
 
class FileTime {   
    /**
     * Unix 时间 1970-01-01 00:00:00 与 Win32 FileTime 时间 1601-01-01 00:00:00
     * 毫秒数差
     */
    public final static long UNIX_FILETIME_DIFF = 11644473600000L;   
    /**
     * Win32 FileTime 采用 100ns 为单位的,定义 100ns 与 1ms 的倍率
     */
    public final static int MILLISECOND_MULTIPLE = 10000;   
    private long low;
    private long high;   
 
    public FileTime() {       
    }
   
    public FileTime(long low, long high) {
        this.low = low;
        this.high = high;
    }
   
    /**
     * 将 Win32 的 FileTime 结构转为 Java 中的 Date 类型
     * @param fileTime
     * @return
     */
    public static Date fileTime2Date(FileTime fileTime) {
        return new Date(fileTime.getFileTime()
                        / FileTime.MILLISECOND_MULTIPLE
                        - FileTime.UNIX_FILETIME_DIFF);
    }
   
    /**
     * 将 Java 中的 Date 类型转为 Win32 的 FileTime 结构
     * @param date
     * @return
     */
    public static FileTime date2FileTime(Date date) {
        long time = (FileTime.UNIX_FILETIME_DIFF
                     + date.getTime()) * FileTime.MILLISECOND_MULTIPLE;
        FileTime fileTime = new FileTime();
        fileTime.setHigh((int)(time >> 32) &0xffffffff);
        fileTime.setLow((int)time&0xffffffff);
        return fileTime;
    }
 
    public long getLow() {
        return low;
    }
    public void setLow(long low) {
        this.low = low;
    }
    public long getHigh() {
        return high;
    }
    public void setHigh(long high) {
        this.high = high;
    }
   
    /**
     * 获得 FileTime 以 64 位数字表示的数据
     * @return
     */
    public long getFileTime() {
        return ((long)high >> 32 & 0xffffffff) | ((long)low & 0xffffffff);
    }
   
    public String toString() {
        return  "high:" + high + "low:" + low;
    }
}

你可能感兴趣的:(java,数据结构,unix)