Java Utililty Methods Formatter Usage

List of utility methods to do Formatter Usage

Description

The list of methods to do Formatter Usage are organized into topic(s).

Method

StringgetCommitCounterStr(final long commitCounter)
Format the commit counter with leading zeros such that it will be lexically ordered in the file system.
final StringBuilder sb = new StringBuilder(21);
final Formatter f = new Formatter(sb);
f.format("%021d", commitCounter);
f.flush();
f.close();
final String basename = sb.toString();
return basename;
StringgetFormatedTime(double timeInSeconds)
get Formated Time
int intTime = (int) timeInSeconds;
final int hours = intTime / 3600;
intTime = intTime % 3600;
final int min = intTime / 60;
intTime = intTime % 60;
final StringBuilder stringBuilder = new StringBuilder();
final Formatter formatter = new Formatter(stringBuilder);
formatter.format("%02d:%02d:%02d", hours, min, intTime);
...
StringgetPropertiesString(Properties myProps)
get Properties String
int maxKeySize = 0;
int maxValueSize = 0;
Enumeration<Object> keys = myProps.keys();
while (keys.hasMoreElements()) {
    String key = (String) keys.nextElement();
    String value = myProps.getProperty(key);
    if (key.length() > maxKeySize)
        maxKeySize = key.length();
...
StringGetRawView(String string)
Get Raw View
byte[] bytes = string.getBytes();
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter();
for (int i = 0; i < bytes.length; i++) {
    String raw = Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1);
    sb.append(raw.toUpperCase());
    if (i != (bytes.length - 1)) {
        sb.append("-");
...
StringgetRoundedString(double value, int powerOf10)
get Rounded String
StringBuilder builder = new StringBuilder();
if (powerOf10 < Integer.MAX_VALUE - 100) {
    value = roundToPowerOf10(value, powerOf10);
Formatter formatter = new Formatter(builder, Locale.getDefault());
String format;
if (powerOf10 < 0) {
    format = "%." + (-powerOf10) + "f";
...
StringincrementAlpha(String suppliedPrefix, String lastNumber, int numberLength)
Returns the next alpha value - ie A00A1 becomes A00A2 etc
String newNumber = "";
String nonNumeric = lastNumber;
Integer value = new Integer(1);
String prefix;
if (suppliedPrefix != null) {
    prefix = suppliedPrefix;
} else {
    prefix = "";
...
StringlistToString(List list, String separator)
Convert a list to a string containing list's elements separated by a separator.
Formatter f = new Formatter();
for (int i = 0; i < list.size(); i++) {
    if (i == list.size() - 1) {
        f.format("%s", list.get(i));
    } else {
        f.format("%s%s", list.get(i), separator);
return f.toString();
Stringlong2Mac(final long macAddress)
long Mac
final StringBuilder result = new StringBuilder(17);
try (Formatter formatter = new Formatter(result)) {
    formatter.format("%02x:%02x:%02x:%02x:%02x:%02x", macAddress >> 40 & 0xff, macAddress >> 32 & 0xff,
            macAddress >> 24 & 0xff, macAddress >> 16 & 0xff, macAddress >> 8 & 0xff, macAddress & 0xff);
return result.toString();
StringmillisToHumanTime(long milliseconds)
Convert milliseconds to human time -the exact format is unspecified
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);
long s = Math.abs(milliseconds / 1000);
long m = Math.abs(milliseconds % 1000);
if (milliseconds > 0) {
    formatter.format("%d.%03ds", s, m);
} else if (milliseconds == 0) {
    formatter.format("0");
...
StringmsToHumanReadableDelta(long start)
Write the given timestamp in a 'nice' format (e.g.h 3h4m instead of 188m)
long stop = System.currentTimeMillis();
return msToHumanReadable(stop - start);