Search in sources :

Example 51 with Formatter

use of java.util.Formatter in project robovm by robovm.

the class OldFormatterTest method test_formatLjava_util_LocaleLjava_lang_StringLjava_lang_Object.

public void test_formatLjava_util_LocaleLjava_lang_StringLjava_lang_Object() {
    Double val = new Double(3.14);
    Calendar cal = Calendar.getInstance();
    Formatter fLoc = null;
    Formatter fNoL = null;
    cal.set(2008, Calendar.SEPTEMBER, 23, 18, 30);
    fLoc = new Formatter(Locale.GERMAN);
    fNoL = new Formatter(Locale.GERMAN);
    fLoc.format(Locale.US, "%f", val);
    fNoL.format("%f", val);
    assertFalse(fLoc.toString().equals(fNoL.toString()));
    fLoc = new Formatter(Locale.FRANCE);
    fNoL = new Formatter(Locale.FRANCE);
    fLoc.format(Locale.US, "%f", val);
    fNoL.format("%f", val);
    assertFalse(fLoc.toString().equals(fNoL.toString()));
    fLoc = new Formatter(Locale.US);
    fNoL = new Formatter(Locale.US);
    fLoc.format(Locale.US, "%f", val);
    fNoL.format("%f", val);
    assertTrue(fLoc.toString().equals(fNoL.toString()));
    fLoc = new Formatter(Locale.GERMAN);
    fNoL = new Formatter(Locale.GERMAN);
    fLoc.format(Locale.US, "%tA %tB %td %tT", cal, cal, cal, cal);
    fNoL.format("%tA %tB %td %tT", cal, cal, cal, cal);
    assertFalse(fLoc.toString().equals(fNoL.toString()));
    fLoc = new Formatter(Locale.FRANCE);
    fNoL = new Formatter(Locale.FRANCE);
    fLoc.format(Locale.US, "%tA %tB %td %tT", cal, cal, cal, cal);
    fNoL.format("%tA %tB %td %tT", cal, cal, cal, cal);
    assertFalse(fLoc.toString().equals(fNoL.toString()));
    fLoc = new Formatter(Locale.US);
    fNoL = new Formatter(Locale.US);
    fLoc.format(Locale.US, "%tA %tB %td %tT", cal, cal, cal, cal);
    fNoL.format("%tA %tB %td %tT", cal, cal, cal, cal);
    assertTrue(fLoc.toString().equals(fNoL.toString()));
    final String[] illFlags = { "%+ e", "%+ E", "%+ g", "%+ G", "%+ f", "%+ a", "%+ A", "%-03e", "%-03E", "%-03g", "%-03G", "%-03f", "%-03a", "%-03A" };
    for (int i = 0; i < illFlags.length; i++) {
        try {
            fLoc = new Formatter(Locale.US);
            fLoc.format(Locale.FRANCE, illFlags[i], 1.23d);
            fail("should throw IllegalFormatFlagsException");
        } catch (IllegalFormatFlagsException expected) {
        }
        try {
            fLoc = new Formatter(Locale.CANADA);
            fLoc.format(Locale.GERMAN, illFlags[i], (Double) null);
            fail("should throw IllegalFormatFlagsException");
        } catch (IllegalFormatFlagsException expected) {
        }
    }
    fLoc.close();
    try {
        fLoc.format(Locale.GERMAN, "%f", val);
        fail();
    } catch (FormatterClosedException expected) {
    }
}
Also used : Formatter(java.util.Formatter) Calendar(java.util.Calendar) FormatterClosedException(java.util.FormatterClosedException) IllegalFormatFlagsException(java.util.IllegalFormatFlagsException)

Example 52 with Formatter

use of java.util.Formatter in project felix by apache.

the class CommandSessionImpl method inspect.

CharSequence inspect(Object b) {
    boolean found = false;
    Formatter f = new Formatter();
    Method[] methods = b.getClass().getMethods();
    for (Method m : methods) {
        try {
            String name = m.getName();
            if (m.getName().startsWith("get") && !m.getName().equals("getClass") && m.getParameterTypes().length == 0 && Modifier.isPublic(m.getModifiers())) {
                found = true;
                name = name.substring(3);
                m.setAccessible(true);
                Object value = m.invoke(b, (Object[]) null);
                f.format(COLUMN, name, format(value, Converter.LINE, this));
            }
        } catch (Exception e) {
        // Ignore
        }
    }
    if (found) {
        return (StringBuilder) f.out();
    } else {
        return b.toString();
    }
}
Also used : Formatter(java.util.Formatter) Method(java.lang.reflect.Method) ExecutionException(java.util.concurrent.ExecutionException)

Example 53 with Formatter

use of java.util.Formatter in project felix by apache.

the class VerifyBundlePlugin method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().info("Skipping Verify execution");
        return;
    }
    Set<String> packagesNotFound = checkPackages();
    if (!packagesNotFound.isEmpty()) {
        Formatter formatter = new Formatter();
        formatter.format("Current bundle %s exports packages that do not exist:%n", project.getArtifact().getFile());
        for (String packageNotFound : packagesNotFound) {
            formatter.format(" * %s%n", packageNotFound);
        }
        formatter.format("Please review the <Export-Package> instruction in the `configuration/instructions` element of the `maven-bundle-plugin`%n");
        formatter.format("For more details, see http://bnd.bndtools.org/heads/export_package.html");
        String message = formatter.toString();
        formatter.close();
        if (failOnError) {
            throw new MojoFailureException(message);
        } else {
            getLog().warn(message);
        }
    }
}
Also used : Formatter(java.util.Formatter) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 54 with Formatter

use of java.util.Formatter in project android by nextcloud.

the class MediaControlView method stringForTime.

private String stringForTime(int timeMs) {
    int totalSeconds = timeMs / 1000;
    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;
    final StringBuilder mFormatBuilder = new StringBuilder();
    final Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
    if (hours > 0) {
        return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
    } else {
        return mFormatter.format("%02d:%02d", minutes, seconds).toString();
    }
}
Also used : Formatter(java.util.Formatter)

Example 55 with Formatter

use of java.util.Formatter in project android_frameworks_base by crdroidandroid.

the class DateUtils method formatElapsedTime.

/**
     * Formats an elapsed time in a format like "MM:SS" or "H:MM:SS" (using a form
     * suited to the current locale), similar to that used on the call-in-progress
     * screen.
     *
     * @param recycle {@link StringBuilder} to recycle, or null to use a temporary one.
     * @param elapsedSeconds the elapsed time in seconds.
     */
public static String formatElapsedTime(StringBuilder recycle, long elapsedSeconds) {
    // Break the elapsed seconds into hours, minutes, and seconds.
    long hours = 0;
    long minutes = 0;
    long seconds = 0;
    if (elapsedSeconds >= 3600) {
        hours = elapsedSeconds / 3600;
        elapsedSeconds -= hours * 3600;
    }
    if (elapsedSeconds >= 60) {
        minutes = elapsedSeconds / 60;
        elapsedSeconds -= minutes * 60;
    }
    seconds = elapsedSeconds;
    // Create a StringBuilder if we weren't given one to recycle.
    // TODO: if we cared, we could have a thread-local temporary StringBuilder.
    StringBuilder sb = recycle;
    if (sb == null) {
        sb = new StringBuilder(8);
    } else {
        sb.setLength(0);
    }
    // Format the broken-down time in a locale-appropriate way.
    // TODO: use icu4c when http://unicode.org/cldr/trac/ticket/3407 is fixed.
    Formatter f = new Formatter(sb, Locale.getDefault());
    initFormatStrings();
    if (hours > 0) {
        return f.format(sElapsedFormatHMMSS, hours, minutes, seconds).toString();
    } else {
        return f.format(sElapsedFormatMMSS, minutes, seconds).toString();
    }
}
Also used : Formatter(java.util.Formatter) RelativeDateTimeFormatter(libcore.icu.RelativeDateTimeFormatter)

Aggregations

Formatter (java.util.Formatter)558 ArrayList (java.util.ArrayList)26 File (java.io.File)25 IOException (java.io.IOException)25 Date (java.util.Date)22 Test (org.junit.Test)19 HashMap (java.util.HashMap)16 Map (java.util.Map)16 AlertDialog (android.app.AlertDialog)14 DialogInterface (android.content.DialogInterface)14 MessageDigest (java.security.MessageDigest)14 PrintWriter (java.io.PrintWriter)13 Justif (aQute.lib.justif.Justif)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 Locale (java.util.Locale)11 BigInteger (java.math.BigInteger)10 PrintStream (java.io.PrintStream)9 Calendar (java.util.Calendar)7 LayoutBuilder (android.text.StaticLayoutTest.LayoutBuilder)6 View (android.view.View)6