java.util.Formatter.toString()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(18.7k)|赞(0)|评价(0)|浏览(202)

本文整理了Java中java.util.Formatter.toString()方法的一些代码示例,展示了Formatter.toString()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Formatter.toString()方法的具体详情如下:
包路径:java.util.Formatter
类名称:Formatter
方法名:toString

Formatter.toString介绍

[英]Returns the content by calling the toString() method of the output destination.
[中]通过调用输出目标的toString()方法返回内容。

代码示例

代码示例来源:origin: stackoverflow.com

public String toDecimalString() {
  Formatter f = new Formatter();
  for(int digit : digits) {
    f.format("%09d", digit);
  }
  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: org.apache.commons/commons-lang3

@Test
public void testDefaultAppend() {
  assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1).toString());
  assertEquals("fo", FormattableUtils.append("foo", new Formatter(), 0, -1, 2).toString());
  assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1).toString());
  assertEquals("   foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1).toString());
  assertEquals(" fo", FormattableUtils.append("foo", new Formatter(), 0, 3, 2).toString());
  assertEquals("   fo", FormattableUtils.append("foo", new Formatter(), 0, 5, 2).toString());
  assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1).toString());
  assertEquals("foo   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1).toString());
  assertEquals("fo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2).toString());
  assertEquals("fo   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2).toString());
}

代码示例来源: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: org.apache.commons/commons-lang3

@Test
public void testAlternatePadCharacter() {
  final char pad='_';
  assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1, pad).toString());
  assertEquals("fo", FormattableUtils.append("foo", new Formatter(), 0, -1, 2, pad).toString());
  assertEquals("_foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1, pad).toString());
  assertEquals("___foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1, pad).toString());
  assertEquals("_fo", FormattableUtils.append("foo", new Formatter(), 0, 3, 2, pad).toString());
  assertEquals("___fo", FormattableUtils.append("foo", new Formatter(), 0, 5, 2, pad).toString());
  assertEquals("foo_", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, pad).toString());
  assertEquals("foo___", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, pad).toString());
  assertEquals("fo_", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, pad).toString());
  assertEquals("fo___", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, pad).toString());
}

代码示例来源: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: 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.commons/commons-lang3

@Test
public void testAlternatePadCharAndEllipsis() {
  assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1, '_', "*").toString());
  assertEquals("f*", FormattableUtils.append("foo", new Formatter(), 0, -1, 2, '_', "*").toString());
  assertEquals("_foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1, '_', "*").toString());
  assertEquals("___foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1, '_', "*").toString());
  assertEquals("_f*", FormattableUtils.append("foo", new Formatter(), 0, 3, 2, '_', "*").toString());
  assertEquals("___f*", FormattableUtils.append("foo", new Formatter(), 0, 5, 2, '_', "*").toString());
  assertEquals("foo_", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, '_', "*").toString());
  assertEquals("foo___", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, '_', "*").toString());
  assertEquals("f*_", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, '_', "*").toString());
  assertEquals("f*___", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, '_', "*").toString());
  assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1, '_', "+*").toString());
  assertEquals("+*", FormattableUtils.append("foo", new Formatter(), 0, -1, 2, '_', "+*").toString());
  assertEquals("_foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1, '_', "+*").toString());
  assertEquals("___foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1, '_', "+*").toString());
  assertEquals("_+*", FormattableUtils.append("foo", new Formatter(), 0, 3, 2, '_', "+*").toString());
  assertEquals("___+*", FormattableUtils.append("foo", new Formatter(), 0, 5, 2, '_', "+*").toString());
  assertEquals("foo_", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, '_', "+*").toString());
  assertEquals("foo___", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, '_', "+*").toString());
  assertEquals("+*_", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, '_', "+*").toString());
  assertEquals("+*___", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, '_', "+*").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: 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: org.apache.commons/commons-lang3

@Test
public void testEllipsis() {
  assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1, "*").toString());
  assertEquals("f*", FormattableUtils.append("foo", new Formatter(), 0, -1, 2, "*").toString());
  assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1, "*").toString());
  assertEquals("   foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1, "*").toString());
  assertEquals(" f*", FormattableUtils.append("foo", new Formatter(), 0, 3, 2, "*").toString());
  assertEquals("   f*", FormattableUtils.append("foo", new Formatter(), 0, 5, 2, "*").toString());
  assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, "*").toString());
  assertEquals("foo   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, "*").toString());
  assertEquals("f* ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, "*").toString());
  assertEquals("f*   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, "*").toString());
  assertEquals("foo", FormattableUtils.append("foo", new Formatter(), 0, -1, -1, "+*").toString());
  assertEquals("+*", FormattableUtils.append("foo", new Formatter(), 0, -1, 2, "+*").toString());
  assertEquals(" foo", FormattableUtils.append("foo", new Formatter(), 0, 4, -1, "+*").toString());
  assertEquals("   foo", FormattableUtils.append("foo", new Formatter(), 0, 6, -1, "+*").toString());
  assertEquals(" +*", FormattableUtils.append("foo", new Formatter(), 0, 3, 2, "+*").toString());
  assertEquals("   +*", FormattableUtils.append("foo", new Formatter(), 0, 5, 2, "+*").toString());
  assertEquals("foo ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, "+*").toString());
  assertEquals("foo   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, "+*").toString());
  assertEquals("+* ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, "+*").toString());
  assertEquals("+*   ", FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, "+*").toString());
}

代码示例来源: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();
  }
}

代码示例来源:origin: com.google.inject/guice

String have = bindingKey.getTypeLiteral().toString();
if (have.contains(want) || want.contains(have)) {
 Formatter fmt = new Formatter();
 Messages.formatSource(fmt, bindingMap.get(bindingKey).getSource());
 String match = String.format("%s bound%s", convert(bindingKey), fmt.toString());
 possibleMatches.add(match);

代码示例来源:origin: lipangit/JiaoZiVideoPlayer

public static String stringForTime(long timeMs) {
  if (timeMs <= 0 || timeMs >= 24 * 60 * 60 * 1000) {
    return "00:00";
  }
  long totalSeconds = timeMs / 1000;
  int seconds = (int) (totalSeconds % 60);
  int minutes = (int) ((totalSeconds / 60) % 60);
  int hours = (int) (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();
  }
}

代码示例来源:origin: org.apache.commons/commons-text

@Test
public void testDefaultAppend() {
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, -1, -1).toString()).isEqualTo("foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, -1, 2).toString()).isEqualTo("fo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 4, -1).toString()).isEqualTo(" foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 6, -1).toString()).isEqualTo("   foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 3, 2).toString()).isEqualTo(" fo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 5, 2).toString()).isEqualTo("   fo");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1).toString()).isEqualTo("foo ");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1).toString()).isEqualTo("foo   ");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2).toString()).isEqualTo("fo ");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2).toString()).isEqualTo("fo   ");
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Log a printf-style formatted message to the channels specified in this RedwoodChannels object.
 * @param format The format string for the printf function
 * @param args The arguments to the printf function
 */
public void logf(String format, Object... args) {
 log((Supplier<String>) () -> new Formatter().format(format, args).toString());
}

代码示例来源:origin: org.apache.commons/commons-text

@Test
public void testAlternatePadCharacter() {
  final char pad = '_';
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, -1, -1, pad).toString()).isEqualTo("foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, -1, 2, pad).toString()).isEqualTo("fo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 4, -1, pad).toString()).isEqualTo("_foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 6, -1, pad).toString()).isEqualTo("___foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 3, 2, pad).toString()).isEqualTo("_fo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 5, 2, pad).toString()).isEqualTo("___fo");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, pad).toString())
    .isEqualTo("foo_");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, pad).toString())
    .isEqualTo("foo___");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, pad).toString())
    .isEqualTo("fo_");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, pad).toString())
    .isEqualTo("fo___");
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Log a printf-style formatted message to the channels specified in this RedwoodChannels object.
 * @param level The log level to log with.
 * @param format The format string for the printf function
 * @param args The arguments to the printf function
 */
public void logf(Flag level, String format, Object... args) {
 log(level, (Supplier<String>) () -> new Formatter().format(format, args).toString());
}

代码示例来源:origin: org.apache.commons/commons-text

@Test
public void testEllipsis() {
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, -1, -1, "*").toString()).isEqualTo("foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, -1, 2, "*").toString()).isEqualTo("f*");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 4, -1, "*").toString()).isEqualTo(" foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 6, -1, "*").toString()).isEqualTo("   foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 3, 2, "*").toString()).isEqualTo(" f*");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 5, 2, "*").toString()).isEqualTo("   f*");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, "*").toString())
    .isEqualTo("foo ");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, "*").toString())
    .isEqualTo("foo   ");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, "*").toString())
    .isEqualTo("f* ");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, "*").toString())
    .isEqualTo("f*   ");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, -1, -1, "+*").toString()).isEqualTo("foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, -1, 2, "+*").toString()).isEqualTo("+*");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 4, -1, "+*").toString()).isEqualTo(" foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 6, -1, "+*").toString()).isEqualTo("   foo");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 3, 2, "+*").toString()).isEqualTo(" +*");
  assertThat(FormattableUtils.append("foo", new Formatter(), 0, 5, 2, "+*").toString()).isEqualTo("   +*");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 4, -1, "+*").toString())
    .isEqualTo("foo ");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 6, -1, "+*").toString())
    .isEqualTo("foo   ");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 3, 2, "+*").toString())
    .isEqualTo("+* ");
  assertThat(FormattableUtils.append("foo", new Formatter(), LEFT_JUSTIFY, 5, 2, "+*").toString())

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * The Redwood equivalent to printf().
 * @param format The format string, as per java's Formatter.format() object.
 * @param args The arguments to format.
 */
public static void logf(String format, Object... args){
 log((Supplier<String>) () -> new Formatter().format(format, args).toString());
}

相关文章