Exemplo n.º 1
0
 @Override
 public Date parse(String i, ParsePosition p) {
   /* delegate to SimpleDateFormat for easy stuff */
   Date d = super.parse(i, p);
   int milliIndex = p.getIndex();
   /* worry aboutthe milliseconds ourselves */
   if (null != d
       && -1 == p.getErrorIndex()
       && milliIndex + 1 < i.length()
       && '.' == i.charAt(milliIndex)) {
     p.setIndex(++milliIndex); // NOTE: ++ to chomp '.'
     Number millis = millisParser.parse(i, p);
     if (-1 == p.getErrorIndex()) {
       int endIndex = p.getIndex();
       d =
           new Date(
               d.getTime()
                   + (long) (millis.doubleValue() * Math.pow(10, (3 - endIndex + milliIndex))));
     }
   }
   return d;
 }
Exemplo n.º 2
0
 @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;
 }