Package java.text

Examples of java.text.FieldPosition


   */
  public static String format (Date d) {
    StringBuffer iso8601 = new StringBuffer();
    SimpleDateFormat sdf = new SimpleDateFormat(masks[2]);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    sdf.format(d, iso8601, new FieldPosition(0));
    return iso8601.toString();
  }
View Full Code Here


      Locale loc = Locale.US;
      Locale.setDefault (loc);

      StringBuffer buf = new StringBuffer ();
      DecimalFormat df = new DecimalFormat ("0.##");
      FieldPosition intPos = new FieldPosition (NumberFormat.INTEGER_FIELD);
      FieldPosition fracPos = new FieldPosition (NumberFormat.FRACTION_FIELD);

      harness.check (format (df, -1234.56, intPos, buf), "-1234.56");
      harness.check (intPos.getBeginIndex (), 1);
      harness.check (intPos.getEndIndex (), 5);

      harness.check (format (df, -1234.56, fracPos, buf), "-1234.56");
      harness.check (fracPos.getBeginIndex (), 6);
      harness.check (fracPos.getEndIndex (), 8);
    }
View Full Code Here

    }
  }
  public String getNumberAsString(Object o){
    if (enableLocalized){
      return formatNumber.format(o, new StringBuffer(),
                    new FieldPosition(0)).toString();
    }
    else {
      return o.toString();
    }
  }
View Full Code Here

     */
    private void readObject(final ObjectInputStream in)
        throws IOException, ClassNotFoundException
    {
        in.defaultReadObject();
        dummy = new FieldPosition(0);
    }
View Full Code Here

   * @return the formatted color string
   */
  public final String format(
    Color color)
  {
    return format(color, new StringBuffer(),new FieldPosition(0)).toString();
  }
View Full Code Here

    private void writeScientific(double value, StringBuffer output,
            Set<StringMod> mods) {

        StringBuffer result = new StringBuffer();
        FieldPosition fractionPos = new FieldPosition(
                DecimalFormat.FRACTION_FIELD);
        decimalFmt.format(value, result, fractionPos);
        writeInteger(result, output, integerSpecials, mods, integerCommas);
        writeFractional(result, output);

        /*
        * Exponent sign handling is complex.
        *
        * In DecimalFormat, you never put the sign in the format, and the sign only
        * comes out of the format if it is negative.
        *
        * In Excel, you always say whether to always show the sign ("e+") or only
        * show negative signs ("e-").
        *
        * Also in Excel, where you put the sign in the format is NOT where it comes
        * out in the result.  In the format, the sign goes with the "e"; in the
        * output it goes with the exponent value.  That is, if you say "#e-|#" you
        * get "1e|-5", not "1e-|5". This makes sense I suppose, but it complicates
        * things.
        *
        * Finally, everything else in this formatting code assumes that the base of
        * the result is the original format, and that starting from that situation,
        * the indexes of the original special characters can be used to place the new
        * characters.  As just described, this is not true for the exponent's sign.
        * <p/>
        * So here is how we handle it:
        *
        * (1) When parsing the format, remove the sign from after the 'e' and put it
        * before the first digit of the exponent (where it will be shown).
        *
        * (2) Determine the result's sign.
        *
        * (3) If it's missing, put the sign into the output to keep the result
        * lined up with the output. (In the result, "after the 'e'" and "before the
        * first digit" are the same because the result has no extra chars to be in
        * the way.)
        *
        * (4) In the output, remove the sign if it should not be shown ("e-" was used
        * and the sign is negative) or set it to the correct value.
        */

        // (2) Determine the result's sign.
        int ePos = fractionPos.getEndIndex();
        int signPos = ePos + 1;
        char expSignRes = result.charAt(signPos);
        if (expSignRes != '-') {
            // not a sign, so it's a digit, and therefore a positive exponent
            expSignRes = '+';
View Full Code Here

     * Tests the {@link RangeFormat#format(Object, StringBuffer, FieldPosition)} method with numbers.
     */
    @Test
    public void testFormatNumbers() {
        format = new RangeFormat(Locale.CANADA);
        minPos = new FieldPosition(RangeFormat.Field.MIN_VALUE);
        maxPos = new FieldPosition(RangeFormat.Field.MAX_VALUE);

        // Closed range
        assertEquals("[-10 … 20]", format(NumberRange.create(-10, true, 20, true)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   4, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 7, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   9, maxPos.getEndIndex());

        // Open range
        assertEquals("(-3 … 4)", format(NumberRange.create(-3, false, 4, false)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   3, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   7, maxPos.getEndIndex());

        // Half-open range
        assertEquals("[2 … 8)", format(NumberRange.create(2, true, 8, false)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   2, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 5, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   6, maxPos.getEndIndex());

        // Half-open range
        assertEquals("(40 … 90]", format(NumberRange.create(40, false, 90, true)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   3, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   8, maxPos.getEndIndex());

        // Single value
        assertEquals("{300}", format(NumberRange.create(300, true, 300, true)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   4, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 1, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   4, maxPos.getEndIndex());

        // Empty range
        assertEquals("{}", format(NumberRange.create(300, true, 300, false)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   1, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 1, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   1, maxPos.getEndIndex());

        // Negative infinity
        assertEquals("(-∞ … 30]", format(NumberRange.create(Double.NEGATIVE_INFINITY, true, 30, true)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   3, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   8, maxPos.getEndIndex());

        // Positive infinity
        assertEquals("[50 … ∞)", format(NumberRange.create(50, true, Double.POSITIVE_INFINITY, true)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   3, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   7, maxPos.getEndIndex());

        // Positive infinities
        assertEquals("(-∞ … ∞)", format(NumberRange.create(Double.NEGATIVE_INFINITY, true, Double.POSITIVE_INFINITY, true)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   3, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   7, maxPos.getEndIndex());

        // Positive infinity with integers
        assertEquals("[50 … ∞)", format(new NumberRange<Integer>(Integer.class, 50, true, null, true)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   3, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   7, maxPos.getEndIndex());

        // Negative infinity with integers
        assertEquals("(-∞ … 40]", format(new NumberRange<Integer>(Integer.class, null, true, 40, true)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   3, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 6, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   8, maxPos.getEndIndex());

        // Measurement
        assertEquals("[-10 … 20] m", format(MeasurementRange.create(-10, true, 20, true, SI.METRE)));
        assertEquals("minPos.beginIndex", 1, minPos.getBeginIndex());
        assertEquals("minPos.endIndex",   4, minPos.getEndIndex());
        assertEquals("maxPos.beginIndex", 7, maxPos.getBeginIndex());
        assertEquals("maxPos.endIndex",   9, maxPos.getEndIndex());

        maxPos = new FieldPosition(RangeFormat.Field.UNIT);
        assertEquals("[-1 … 2] km", format(MeasurementRange.create(-1, true, 2, true, SI.KILOMETRE)));
        assertEquals("unitPos.beginIndex", 9, maxPos.getBeginIndex());
        assertEquals("unitPos.endIndex"11, maxPos.getEndIndex());
    }
View Full Code Here

     * using the alternate format.
     */
    @Test
    public void testAlternateFormat() {
        format = new RangeFormat(Locale.CANADA);
        minPos = new FieldPosition(RangeFormat.Field.MIN_VALUE);
        maxPos = new FieldPosition(RangeFormat.Field.MAX_VALUE);
        format.setAlternateForm(true);

        assertEquals("[-10 … 20]", format(NumberRange.create(-10, true, 20, true)));
        assertEquals("]-3 … 4[",   format(NumberRange.create( -3, false, 4, false)));
        assertEquals("[2 … 8[",    format(NumberRange.create2, true,  8, false)));
View Full Code Here

     * Tests the {@link RangeFormat#formatToCharacterIterator(Object)} method with dates.
     */
    @Test
    public void testFormatDatesToCharacterIterator() {
        format   = new RangeFormat(Locale.FRANCE, TimeZone.getTimeZone("UTC"));
        minPos   = new FieldPosition(RangeFormat.Field.MIN_VALUE);
        maxPos   = new FieldPosition(RangeFormat.Field.MAX_VALUE);
        parsePos = new ParsePosition(0);

        final long HOUR = 60L * 60 * 1000;
        final long DAY  = 24L * HOUR;
        final long YEAR = round(365.25 * DAY);
 
View Full Code Here

         * programmatically by a call to 'setTimeFormat()', or in logging.properties
         * file with the "org.apache.sis.util.logging.MonolineFormatter.time" property.
         */
        if (timeFormat != null) {
            Date time = new Date(Math.max(0, record.getMillis() - startMillis));
            timeFormat.format(time, buffer, new FieldPosition(0));
            buffer.append(' ');
        }
        /*
         * Appends the level (e.g. "FINE"). We do not provide the option to turn level off for now.
         * This level will be formatted with a colorized background if ANSI escape sequences are enabled.
View Full Code Here

TOP

Related Classes of java.text.FieldPosition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.