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) {
}
}
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();
}
}
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);
}
}
}
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();
}
}
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();
}
}
Aggregations