Example #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();
  }
Example #2
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();
  }
 /**
  * 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;
 }