Skip to content

Instantly share code, notes, and snippets.

@chankruze
Created November 12, 2019 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chankruze/55242e3e0e5363ddcebb5ab6f83facef to your computer and use it in GitHub Desktop.
Save chankruze/55242e3e0e5363ddcebb5ab6f83facef to your computer and use it in GitHub Desktop.
@SuppressLint("DefaultLocale")
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
public String getReadableSize() {
long size = this.length();
if (size <= 0) return "0";
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
int unitGroup = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, unitGroup))
+ " " + units[unitGroup];
}
public String getLastModified() {
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("dd/MM/yyyy EEE HH:mm", Locale.getDefault());
return simpleDateFormat.format(this.lastModified());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment