Example usage for java.util Formatter Formatter

List of usage examples for java.util Formatter Formatter

Introduction

In this page you can find the example usage for java.util Formatter Formatter.

Prototype

public Formatter() 

Source Link

Document

Constructs a new formatter.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();

    // Right justify by default 
    fmt.format("|%10.2f|", 123.123);
    System.out.println(fmt);//from  ww  w.j ava 2 s . c  o  m

    // Now, left justify. 
    fmt = new Formatter();
    fmt.format("|%-10.2f|", 123.123);
    System.out.println(fmt);
}

From source file:MainClass.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();

    // Format 4 decimal places.
    fmt.format("%.4f", 123.1234567);
    System.out.println(fmt);/*from   www .jav  a 2 s  . c om*/

    // Format to 2 decimal places in a 16 character field.
    fmt = new Formatter();
    fmt.format("%16.2e", 123.1234567);
    System.out.println(fmt);

    // Display at most 15 characters in a string.
    fmt = new Formatter();
    fmt.format("%.15s", "Formatting with Java is now easy.");
    System.out.println(fmt);
}

From source file:MainClass.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();

    fmt.format("Today is day %te of %<tB, %<tY", cal);
    System.out.println(fmt);/*  w w  w  .j a  v  a 2 s  .  c  o  m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    double data[] = { 12.3, 45.6, -7.89, -1.0, 1.01 };
    Formatter fmt = new Formatter();

    fmt.format("%12s %12s\n", "Value", "Cube Root");

    for (double v : data) {
        fmt.format("%12.4f %12.4f\n", v, Math.cbrt(v));
    }/*w  ww .  j  a v  a2  s. c o m*/
    System.out.println(fmt);
}

From source file:MainClass.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();

    for (double i = 1000; i < 1.0e+10; i *= 100) {
        fmt.format("%g ", i);
        System.out.println(fmt);/*from  www. j a  v a  2  s .  co m*/
    }

}

From source file:MainClass.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();
    Calendar cal = Calendar.getInstance();

    fmt.format("%tr", cal);
    System.out.println(fmt);//from w  w  w .ja v  a 2 s . com

    fmt = new Formatter();
    fmt.format("%tc", cal);
    System.out.println(fmt);

    fmt = new Formatter();
    fmt.format("%tl:%tM", cal, cal);
    System.out.println(fmt);

    fmt = new Formatter();
    fmt.format("%tB %tb %tm", cal, cal, cal);
    System.out.println(fmt);
}

From source file:Main.java

public static String transferTP(String oldValue) {
    String newValue = oldValue;/*from  w  w w  . j  a  v  a  2  s.  com*/
    float ov = Float.valueOf(oldValue) / 1000;
    Formatter fmt = new Formatter();

    if (ov >= 100) {
        newValue = String.valueOf(Math.floor(ov + 0.5));
    } else if (ov > 10) {
        newValue = fmt.format("%.1f", ov).toString();
    } else {
        newValue = fmt.format("%.2f", ov).toString();
    }

    return newValue;
}

From source file:Main.java

public static String toSHA1(String s) {
    MessageDigest md = null;//from w  w w  .  j  a  v a2s.c  o m
    byte[] sha1hash = null;
    try {
        md = MessageDigest.getInstance("SHA-1");
        sha1hash = new byte[40];
        md.update(s.getBytes(UTF_8), 0, s.length());
    } catch (Exception e) {
        return ERROR_SHA1;
    }
    sha1hash = md.digest();
    Formatter formatter = new Formatter();
    for (byte b : sha1hash) {
        formatter.format("%02x", b);
    }
    return formatter.toString();
}

From source file:Main.java

public static String stringForTime(int timeMs) {
    int totalSeconds = timeMs / 1000;

    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;

    Formatter mFormatter = new Formatter();
    if (hours > 0) {
        return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
    } else {//from   w ww  . j av  a 2 s  . co  m
        return mFormatter.format("%02d:%02d", minutes, seconds).toString();
    }
}

From source file:pl.p.lodz.ftims.server.logic.AuthenticationUtils.java

private String bytesToHex(byte[] bytes) {
    try (Formatter formatter = new Formatter()) {
        for (byte b : bytes) {
            formatter.format("%02x", b);
        }//from   ww w .ja  v a2 s .c  om
        return formatter.toString();
    }
}