本文整理了Java中java.util.Formatter.format()
方法的一些代码示例,展示了Formatter.format()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Formatter.format()
方法的具体详情如下:
包路径:java.util.Formatter
类名称:Formatter
方法名:format
[英]Writes a formatted string to the output destination of the Formatter.
[中]将格式化字符串写入格式化程序的输出目标。
代码示例来源:origin: stackoverflow.com
public String toDecimalString() {
Formatter f = new Formatter();
f.format("%d", digits[0]);
for(int i = 1 ; i < digits.length; i++) {
f.format("%09d", digits[i]);
}
return f.toString();
}
代码示例来源:origin: TeamNewPipe/NewPipe
public static String getTimeString(int milliSeconds) {
long seconds = (milliSeconds % 60000L) / 1000L;
long minutes = (milliSeconds % 3600000L) / 60000L;
long hours = (milliSeconds % 86400000L) / 3600000L;
long days = (milliSeconds % (86400000L * 7L)) / 86400000L;
stringBuilder.setLength(0);
return days > 0 ? stringFormatter.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds).toString()
: hours > 0 ? stringFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString()
: stringFormatter.format("%02d:%02d", minutes, seconds).toString();
}
代码示例来源:origin: knowm/XChange
private static String byteToHex(final byte[] hash) {
Formatter formatter = new Formatter();
for (byte b : hash) {
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
}
代码示例来源:origin: vert-x3/vertx-examples
Formatter formatter = new Formatter(buf);
formatter.format(format,
"ID",
"STATE",
} else {
int index = i - 2;
if (index < threads.size()) {
Thread thread = threads.get(index);
formatter.format(format,
thread.getId(),
thread.getState().name(),
代码示例来源:origin: org.assertj/assertj-core
private static String escapeUnicode(String input) {
StringBuilder b = new StringBuilder(input.length());
Formatter formatter = new Formatter(b);
for (char c : input.toCharArray()) {
if (c < 128) {
b.append(c);
} else {
formatter.format("\\u%04x", (int) c);
}
}
formatter.close();
return b.toString();
}
}
代码示例来源:origin: wildfly/wildfly
public JBossLogPrintWriter format(String format, Object ... args) {
synchronized (lock) {
if ((formatter == null)
|| (formatter.locale() != Locale.getDefault()))
formatter = new Formatter(this);
formatter.format(Locale.getDefault(), format, args);
}
return this;
}
代码示例来源:origin: org.apache.poi/poi
/** {@inheritDoc} */
public void formatValue(StringBuffer toAppendTo, Object value) {
double elapsed = ((Number) value).doubleValue();
if (elapsed < 0) {
toAppendTo.append('-');
elapsed = -elapsed;
}
Object[] parts = new Long[specs.size()];
for (int i = 0; i < specs.size(); i++) {
parts[i] = specs.get(i).valueFor(elapsed);
}
Formatter formatter = new Formatter(toAppendTo, Locale.ROOT);
try {
formatter.format(printfFmt, parts);
} finally {
formatter.close();
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
Formatter formatter = new Formatter(treeDebugLine);
boolean isDone = (Math.abs(bestTree.getScore() - goldTree.getScore()) <= 0.00001 || goldTree.getScore() > bestTree.getScore());
String done = isDone ? "done" : "";
formatter.format("Tree %6d Highest tree: %12.4f Correct tree: %12.4f %s", treeNum, bestTree.getScore(), goldTree.getScore(), done);
log.info(treeDebugLine.toString());
if (!isDone){
value = (1.0/trainingBatch.size()) * value;
ArrayMath.multiplyInPlace(derivative, (1.0/trainingBatch.size()));
代码示例来源:origin: stackoverflow.com
public String toDecimalString() {
Formatter f = new Formatter();
for(int digit : digits) {
f.format("%09d", digit);
}
return f.toString();
}
代码示例来源:origin: joel-costigliola/assertj-core
private static String escapeUnicode(String input) {
StringBuilder b = new StringBuilder(input.length());
Formatter formatter = new Formatter(b);
for (char c : input.toCharArray()) {
if (c < 128) {
b.append(c);
} else {
formatter.format("\\u%04x", (int) c);
}
}
formatter.close();
return b.toString();
}
}
代码示例来源:origin: wildfly/wildfly
public JBossLogPrintWriter format(Locale l, String format, Object ... args) {
synchronized (lock) {
if ((formatter == null) || (formatter.locale() != l))
formatter = new Formatter(this, l);
formatter.format(l, format, args);
}
return this;
}
代码示例来源:origin: google/ExoPlayer
/**
* Returns the specified millisecond time formatted as a string.
*
* @param builder The builder that {@code formatter} will write to.
* @param formatter The formatter.
* @param timeMs The time to format as a string, in milliseconds.
* @return The time formatted as a string.
*/
public static String getStringForTime(StringBuilder builder, Formatter formatter, long timeMs) {
if (timeMs == C.TIME_UNSET) {
timeMs = 0;
}
long totalSeconds = (timeMs + 500) / 1000;
long seconds = totalSeconds % 60;
long minutes = (totalSeconds / 60) % 60;
long hours = totalSeconds / 3600;
builder.setLength(0);
return hours > 0 ? formatter.format("%d:%02d:%02d", hours, minutes, seconds).toString()
: formatter.format("%02d:%02d", minutes, seconds).toString();
}
代码示例来源:origin: com.google.inject/guice
Formatter fmt = new Formatter().format(heading).format(":%n%n");
int index = 1;
boolean displayCauses = getOnlyCause(errorMessages) == null;
for (Message errorMessage : errorMessages) {
int thisIdx = index++;
fmt.format("%s) %s%n", thisIdx, errorMessage.getMessage());
for (int i = dependencies.size() - 1; i >= 0; i--) {
Object source = dependencies.get(i);
formatSource(fmt, source);
if (!causes.containsKey(causeEquivalence)) {
causes.put(causeEquivalence, thisIdx);
fmt.format("Caused by: %s", Throwables.getStackTraceAsString(cause));
} else {
int causeIdx = causes.get(causeEquivalence);
fmt.format(
"Caused by: %s (same stack trace as error #%s)",
cause.getClass().getName(), causeIdx);
fmt.format("%n");
fmt.format("1 error");
} else {
fmt.format("%s errors", errorMessages.size());
return fmt.toString();
代码示例来源:origin: apache/accumulo
public void print(ReadOnlyTStore<T> zs, IZooReader zk, String lockPath, Formatter fmt,
Set<Long> filterTxid, EnumSet<TStatus> filterStatus)
throws KeeperException, InterruptedException {
FateStatus fateStatus = getStatus(zs, zk, lockPath, filterTxid, filterStatus);
for (TransactionStatus txStatus : fateStatus.getTransactions()) {
fmt.format("txid: %s status: %-18s op: %-15s locked: %-15s locking: %-15s top: %s%n",
txStatus.getTxid(), txStatus.getStatus(), txStatus.getDebug(), txStatus.getHeldLocks(),
txStatus.getWaitingLocks(), txStatus.getTop());
}
fmt.format(" %s transactions", fateStatus.getTransactions().size());
if (fateStatus.getDanglingHeldLocks().size() != 0
|| fateStatus.getDanglingWaitingLocks().size() != 0) {
fmt.format("%nThe following locks did not have an associated FATE operation%n");
for (Entry<String,List<String>> entry : fateStatus.getDanglingHeldLocks().entrySet())
fmt.format("txid: %s locked: %s%n", entry.getKey(), entry.getValue());
for (Entry<String,List<String>> entry : fateStatus.getDanglingWaitingLocks().entrySet())
fmt.format("txid: %s locking: %s%n", entry.getKey(), entry.getValue());
}
}
代码示例来源:origin: jersey/jersey
@Override
public String toString() {
return (new Formatter()).format(
"{\"flightId\":\"%s\",\"company\":\"%s\",\"number\":%d,\"aircraft\":\"%s\"}",
this.flightId, this.company, this.number, this.aircraft).toString();
}
}
代码示例来源:origin: org.apache.poi/poi
private void writeSingleInteger(String fmt, int num, StringBuffer output, List<Special> numSpecials, Set<CellNumberStringMod> mods) {
StringBuffer sb = new StringBuffer();
Formatter formatter = new Formatter(sb, locale);
try {
formatter.format(locale, fmt, num);
} finally {
formatter.close();
}
writeInteger(sb, output, numSpecials, mods, false);
}
代码示例来源:origin: patric-r/jvmtop
/**
* Formats number of milliseconds to a HH:MM representation
*
* TODO: implement automatic scale (e.g. 1d 7h instead of 31:13m)
* @param millis
* @return
*/
public String toHHMM(long millis)
{
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
formatter
.format("%2d:%2dm", millis / 1000 / 3600,
(millis / 1000 / 60) % 60);
return sb.toString();
}
代码示例来源:origin: brianwernick/ExoMedia
/**
* Formats the specified milliseconds to a human readable format
* in the form of (Hours : Minutes : Seconds). If the specified
* milliseconds is less than 0 the resulting format will be
* "--:--" to represent an unknown time
*
* @param milliseconds The time in milliseconds to format
* @return The human readable time
*/
public static String formatMs(long milliseconds) {
if (milliseconds < 0) {
return "--:--";
}
long seconds = (milliseconds % DateUtils.MINUTE_IN_MILLIS) / DateUtils.SECOND_IN_MILLIS;
long minutes = (milliseconds % DateUtils.HOUR_IN_MILLIS) / DateUtils.MINUTE_IN_MILLIS;
long hours = (milliseconds % DateUtils.DAY_IN_MILLIS) / DateUtils.HOUR_IN_MILLIS;
formatBuilder.setLength(0);
if (hours > 0) {
return formatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
}
return formatter.format("%02d:%02d", minutes, seconds).toString();
}
}
代码示例来源:origin: stackoverflow.com
Formatter formatter = new Formatter();
for (byte b : hash)
formatter.format("%02x", b);
String result = formatter.toString();
formatter.close();
return result;
代码示例来源:origin: CarGuo/GSYVideoPlayer
public static String stringForTime(int timeMs) {
if (timeMs <= 0 || timeMs >= 24 * 60 * 60 * 1000) {
return "00:00";
}
int totalSeconds = timeMs / 1000;
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
StringBuilder stringBuilder = new StringBuilder();
Formatter mFormatter = new Formatter(stringBuilder, Locale.getDefault());
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
内容来源于网络,如有侵权,请联系作者删除!