首页 > 代码库 > 单位换算(格式化十进制数-B),获取时间工具类CommenUtil

单位换算(格式化十进制数-B),获取时间工具类CommenUtil

package com.example.administrator.filemanager.utils;


import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/12/29.
 */

public class CommonUtils {
    //DecimalFormat:用于格式化十进制的数字
    private static DecimalFormat df = new DecimalFormat("#.00");
    /**
     * 单位换算,单位:B
     */
    public static String getFileSize(long filename){
        StringBuffer buffer = new StringBuffer();
        if (filename<1024){//小于1KB  1kb = 1024b
            buffer.append(filename);
            buffer.append("B");
        }else if (filename< 1048576 ){//小于 1M 1M = 1024*1024 b
            buffer.append(df.format((double) filename/1024));
            buffer.append("K");
        }else if(filename< 1073741824){//小于 1G 1G = 1024*1024*1024 b
            buffer.append(df.format((double) filename/1048576));
            buffer.append("M");
        }else{//大于 1G 1G = 1024*1024*1024 b
            buffer.append(df.format((double) filename/1073741824));
            buffer.append("G");
        }

        return buffer.toString();
    }

    /**
     * 获取时间
     */
    public static  String getStrTime(long filename){

        if (filename==0){
            return "未知";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String str = format.format(new Date(filename));

        return str;
    }

}


单位换算(格式化十进制数-B),获取时间工具类CommenUtil