Пример #1
0
  public static String formatElapsedTime(long elapsedTime) {
    StringBuilder buffer = new StringBuilder();

    if (elapsedTime < 60) {
      buffer.append("       - ");
    } else {
      long hours = elapsedTime / 3600;
      if (hours > 0) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        FieldPosition fp = new FieldPosition(NumberFormat.INTEGER_FIELD);
        nf.setMaximumIntegerDigits(3);
        String h = nf.format(hours, new StringBuffer(), fp).toString();
        buffer.append(StringUtils.repeatString(" ", 3 - fp.getEndIndex())).append(h).append("h ");
        elapsedTime -= 3600 * hours;
      } else {
        buffer.append("     ");
      }

      long minutes = elapsedTime / 60;
      if (minutes < 10) {
        buffer.append(' ');
      }
      buffer.append(minutes).append("m");
    }

    return buffer.toString();
  }
Пример #2
0
 @Override
 public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
   Objects.requireNonNull(obj, "obj");
   Objects.requireNonNull(toAppendTo, "toAppendTo");
   Objects.requireNonNull(pos, "pos");
   if (obj instanceof DateTimeAccessor == false) {
     throw new IllegalArgumentException("Format target must implement DateTimeAccessor");
   }
   pos.setBeginIndex(0);
   pos.setEndIndex(0);
   try {
     formatter.printTo((DateTimeAccessor) obj, toAppendTo);
   } catch (RuntimeException ex) {
     throw new IllegalArgumentException(ex.getMessage(), ex);
   }
   return toAppendTo;
 }
Пример #3
0
  public static String formatEventTime(final Date eventTime) {
    StringBuilder buffer = new StringBuilder();
    long now = System.currentTimeMillis();
    long days = (now - eventTime.getTime()) / 86400000L; // milliseconds in a day
    if (days > 0) {
      NumberFormat nf = NumberFormat.getNumberInstance();
      FieldPosition fp = new FieldPosition(NumberFormat.INTEGER_FIELD);
      nf.setMaximumIntegerDigits(3);
      String ds = nf.format(days, new StringBuffer(), fp).toString();
      buffer.append(StringUtils.repeatString(" ", 3 - fp.getEndIndex())).append(ds).append('+');
    } else {
      buffer.append("    ");
    }

    SimpleDateFormat df = new SimpleDateFormat("hh:mm aa");
    df.setTimeZone(TimeZone.getDefault());
    buffer.append(df.format(eventTime));

    return buffer.toString();
  }
 @Override
 public StringBuffer format(Date d, StringBuffer toAppendTo, FieldPosition pos) {
   /* delegate to SimpleDateFormat for easy stuff */
   super.format(d, toAppendTo, pos);
   /* worry aboutthe milliseconds ourselves */
   long millis = d.getTime() % 1000l;
   if (0L == millis) {
     return toAppendTo;
   }
   if (millis < 0L) {
     // original date was prior to epoch
     millis += 1000L;
   }
   int posBegin = toAppendTo.length();
   toAppendTo.append(millisFormat.format(millis / 1000d));
   if (DateFormat.MILLISECOND_FIELD == pos.getField()) {
     pos.setBeginIndex(posBegin);
     pos.setEndIndex(toAppendTo.length());
   }
   return toAppendTo;
 }
Пример #5
0
 /**
  * Note: breaks functionality of fieldPosition param. Also: there's a bug in SimpleDateFormat with
  * "S" and "SS", use "SSS" instead if you want a msec field.
  */
 public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
   long dt = date.getTime();
   long ds = dt / 1000;
   if (ds != lastSec) {
     sb.setLength(0);
     df.format(date, sb, fp);
     lastSec = ds;
   } else {
     // munge current msec into existing string
     int ms = (int) (dt % 1000);
     int pos = fp.getEndIndex();
     int begin = fp.getBeginIndex();
     if (pos > 0) {
       if (pos > begin) sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
       ms /= 10;
       if (pos > begin) sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
       ms /= 10;
       if (pos > begin) sb.setCharAt(--pos, Character.forDigit(ms % 10, 10));
     }
   }
   toAppendTo.append(sb.toString());
   return toAppendTo;
 }
Пример #6
0
  @Override
  public StringBuffer format(Object value, StringBuffer buffer, FieldPosition field) {

    if (!(value instanceof Number)) {
      throw new IllegalArgumentException();
    }
    if (buffer == null || field == null) {
      throw new NullPointerException();
    }

    String fieldType = null;
    if (field != null) {
      fieldType = getFieldType(field.getFieldAttribute());
    }

    Number number = (Number) value;

    if (number instanceof BigInteger) {
      BigInteger valBigInteger = (BigInteger) number;
      String result =
          NativeDecimalFormat.format(
              this.addr, valBigInteger.toString(10), field, fieldType, null, 0);
      return buffer.append(result);
    } else if (number instanceof BigDecimal) {
      BigDecimal valBigDecimal = (BigDecimal) number;
      String result =
          NativeDecimalFormat.format(
              this.addr,
              valBigDecimal.unscaledValue().toString(10),
              field,
              fieldType,
              null,
              valBigDecimal.scale());
      return buffer.append(result);
    } else {
      double dv = number.doubleValue();
      long lv = number.longValue();
      if (dv == lv) {
        String result = NativeDecimalFormat.format(this.addr, lv, field, fieldType, null);
        return buffer.append(result);
      }
      String result = NativeDecimalFormat.format(this.addr, dv, field, fieldType, null);
      return buffer.append(result);
    }
  }
Пример #7
0
  @Override
  public StringBuffer format(double value, StringBuffer buffer, FieldPosition field) {

    if (buffer == null) {
      throw new NullPointerException();
    }

    String fieldType = null;

    if (field != null) {
      fieldType = getFieldType(field.getFieldAttribute());
    }

    String result = NativeDecimalFormat.format(this.addr, value, field, fieldType, null);

    buffer.append(result.toCharArray(), 0, result.length());

    return buffer;
  }