Java Timestamp Format formatFileSize(Number data)

Here you can find the source of formatFileSize(Number data)

Description

format File Size

License

Apache License

Declaration

public static String formatFileSize(Number data) 

Method Source Code


//package com.java2s;
/*//from w ww .j  a  v  a  2s.  c o  m
 * Copyright (C) 2010-2101 Alibaba Group Holding Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    private static final String PATTERN = "#,###.###";
    private static final Long KB_SIZE = 1024L;
    private static final Long MB_SIZE = 1024 * KB_SIZE;
    private static final Long GB_SIZE = 1024 * MB_SIZE;
    private static final Long TB_SIZE = 1024 * GB_SIZE;

    public static String formatFileSize(Number data) {
        if (data == null) {
            return null;
        }

        long size = data.longValue();
        if (size > TB_SIZE) {
            DecimalFormat format = new DecimalFormat(PATTERN);
            return format.format((size * 1.0) / TB_SIZE) + " TB";
        } else if (size > GB_SIZE) {
            DecimalFormat format = new DecimalFormat(PATTERN);
            return format.format((size * 1.0) / GB_SIZE) + " GB";
        } else if (size > MB_SIZE) {
            DecimalFormat format = new DecimalFormat(PATTERN);
            return format.format((size * 1.0) / MB_SIZE) + " MB";
        } else if (size > KB_SIZE) {
            DecimalFormat format = new DecimalFormat(PATTERN);
            return format.format((size * 1.0) / KB_SIZE) + " KB";
        } else {
            DecimalFormat format = new DecimalFormat(PATTERN);
            return format.format(size) + " B";
        }

    }

    public static String format(Double data) {
        if (data == null) {
            return null;
        }
        DecimalFormat format = new DecimalFormat(PATTERN);
        return format.format(data);
    }

    public static String format(Integer data) {
        if (data == null) {
            return null;
        }
        DecimalFormat format = new DecimalFormat(PATTERN);
        return format.format(data);
    }

    public static String format(Long data) {
        if (data == null) {
            return null;
        }
        DecimalFormat format = new DecimalFormat(PATTERN);
        return format.format(data);
    }

    public static String format(BigDecimal data) {
        if (data == null) {
            return null;
        }

        DecimalFormat format = new DecimalFormat(PATTERN);
        return format.format(data);
    }

    public static String format(BigInteger data) {
        if (data == null) {
            return null;
        }

        DecimalFormat format = new DecimalFormat(PATTERN);
        return format.format(data);
    }

    public static String format(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String format(java.sql.Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String format(java.sql.Time time) {
        if (time == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        return format.format(time);
    }

    public static String format(java.sql.Timestamp timestamp) {
        if (timestamp == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:fffffffff");
        return format.format(timestamp);
    }
}

Related

  1. formatDateToUnixTimestamp(Date date)
  2. formatDateWithCinderellaTime(Date date)
  3. formatDefaultDate(Timestamp time)
  4. formatDuration(final long duration)
  5. formatElapsed(long elapsed)
  6. formatGMTTimestampToDate(Timestamp time)
  7. formatJDBCTimeStamp(final java.util.Date date)
  8. formatJDBCTimeStamp(final java.util.Date date)
  9. formatMillis(long _millis)