Search in sources :

Example 16 with Format

use of java.text.Format in project poi by apache.

the class TestHSSFDataFormatter method testSetDefaultNumberFormat.

/**
     * Test using a default number format. The format should be used when a
     * format pattern cannot be parsed by DecimalFormat.
     */
@Test
public void testSetDefaultNumberFormat() {
    HSSFRow row = wb.getSheetAt(0).getRow(3);
    Iterator<Cell> it = row.cellIterator();
    DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale());
    Format defaultFormat = new DecimalFormat("Balance $#,#00.00 USD;Balance -$#,#00.00 USD", dfs);
    formatter.setDefaultNumberFormat(defaultFormat);
    log("\n==== DEFAULT NUMBER FORMAT ====");
    while (it.hasNext()) {
        Cell cell = it.next();
        cell.setCellValue(cell.getNumericCellValue() * Math.random() / 1000000 - 1000);
        log(formatter.formatCellValue(cell));
        String formatted = formatter.formatCellValue(cell);
        assertTrue("Doesn't start with Balance: " + formatted, formatted.startsWith("Balance "));
        assertTrue("Doesn't end with USD: " + formatted, formatted.endsWith(" USD"));
    }
}
Also used : Format(java.text.Format) DecimalFormat(java.text.DecimalFormat) SimpleDateFormat(java.text.SimpleDateFormat) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) Cell(org.apache.poi.ss.usermodel.Cell) Test(org.junit.Test)

Example 17 with Format

use of java.text.Format in project robovm by robovm.

the class OldMessageFormatTest method test_setFormats$Ljava_text_Format.

public void test_setFormats$Ljava_text_Format() {
    try {
        MessageFormat f1 = (MessageFormat) format1.clone();
        // case 1: Test with repeating formats and max argument index < max
        // offset
        // compare getFormats() results after calls to setFormats(Format[])
        Format[] correctFormats = new Format[] { DateFormat.getTimeInstance(), new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(), NumberFormat.getCurrencyInstance(), new ChoiceFormat("1#few|2#ok|3#a lot") };
        f1.setFormats(correctFormats);
        Format[] formats = f1.getFormats();
        assertTrue("Test1A:Returned wrong number of formats:", correctFormats.length <= formats.length);
        for (int i = 0; i < correctFormats.length; i++) {
            assertEquals("Test1B:wrong format for argument index " + i + ":", correctFormats[i], formats[i]);
        }
        // case 2: Try to pass null argument to setFormats().
        try {
            f1.setFormats(null);
            fail("Expected exception NullPointerException was not thrown");
        } catch (NullPointerException e) {
        // expected
        }
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
}
Also used : Format(java.text.Format) NumberFormat(java.text.NumberFormat) MessageFormat(java.text.MessageFormat) Support_MessageFormat(tests.support.Support_MessageFormat) ChoiceFormat(java.text.ChoiceFormat) DateFormat(java.text.DateFormat) MessageFormat(java.text.MessageFormat) Support_MessageFormat(tests.support.Support_MessageFormat) ChoiceFormat(java.text.ChoiceFormat) ParseException(java.text.ParseException)

Example 18 with Format

use of java.text.Format in project robovm by robovm.

the class OldMessageFormatTest method test_setFormatILjava_text_Format.

/**
     * java.text.MessageFormat#setFormat(int, Format) Test of method
     *        java.text.MessageFormat#setFormat(int, Format). Case 1: Compare
     *        getFormats() results after calls to setFormat(). Case 2: Try to
     *        call setFormat() using incorrect index.
     */
public void test_setFormatILjava_text_Format() {
    try {
        // case 1: Compare getFormats() results after calls to setFormat()
        MessageFormat f1 = (MessageFormat) format1.clone();
        f1.setFormat(0, DateFormat.getTimeInstance());
        f1.setFormat(1, DateFormat.getTimeInstance());
        f1.setFormat(2, NumberFormat.getInstance());
        f1.setFormat(3, new ChoiceFormat("0#off|1#on"));
        f1.setFormat(4, new ChoiceFormat("1#few|2#ok|3#a lot"));
        f1.setFormat(5, DateFormat.getTimeInstance());
        Format[] formats = f1.getFormats();
        formats = f1.getFormats();
        Format[] correctFormats = new Format[] { DateFormat.getTimeInstance(), DateFormat.getTimeInstance(), NumberFormat.getInstance(), new ChoiceFormat("0#off|1#on"), new ChoiceFormat("1#few|2#ok|3#a lot"), DateFormat.getTimeInstance() };
        assertEquals("Test1A:Returned wrong number of formats:", correctFormats.length, formats.length);
        for (int i = 0; i < correctFormats.length; i++) {
            assertEquals("Test1B:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]);
        }
        // case 2: Try to setFormat using incorrect index
        try {
            f1.setFormat(-1, DateFormat.getDateInstance());
            fail("Expected ArrayIndexOutOfBoundsException was not thrown");
            f1.setFormat(f1.getFormats().length, DateFormat.getDateInstance());
            fail("Expected ArrayIndexOutOfBoundsException was not thrown");
        } catch (ArrayIndexOutOfBoundsException e) {
        // expected
        }
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
}
Also used : Format(java.text.Format) NumberFormat(java.text.NumberFormat) MessageFormat(java.text.MessageFormat) Support_MessageFormat(tests.support.Support_MessageFormat) ChoiceFormat(java.text.ChoiceFormat) DateFormat(java.text.DateFormat) MessageFormat(java.text.MessageFormat) Support_MessageFormat(tests.support.Support_MessageFormat) ChoiceFormat(java.text.ChoiceFormat) ParseException(java.text.ParseException)

Example 19 with Format

use of java.text.Format in project robovm by robovm.

the class OldFormatTest method test_clone.

/**
     * java.text.Format#clone() Test of method java.text.Format#clone().
     *        Compare of internal variables of cloned objects.
     */
public void test_clone() {
    // Compare of internal variables of cloned objects
    Format fm = new MockFormat();
    Format fmc = (Format) fm.clone();
    assertEquals(fm.getClass(), fmc.getClass());
}
Also used : Format(java.text.Format)

Example 20 with Format

use of java.text.Format in project sessdb by ppdai.

the class DateFormatter method formatCurrentDate.

public static String formatCurrentDate() {
    Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String sDate = formatter.format(new Date());
    return sDate;
}
Also used : Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

Format (java.text.Format)146 SimpleDateFormat (java.text.SimpleDateFormat)60 DecimalFormat (java.text.DecimalFormat)39 Test (org.junit.Test)38 DateFormat (java.text.DateFormat)34 NumberFormat (java.text.NumberFormat)29 DateTimeFormatter (java.time.format.DateTimeFormatter)27 Date (java.util.Date)26 ChoiceFormat (java.text.ChoiceFormat)24 ParsePosition (java.text.ParsePosition)22 MessageFormat (java.text.MessageFormat)20 FieldPosition (java.text.FieldPosition)16 Test (org.testng.annotations.Test)16 IOException (java.io.IOException)14 ParseException (java.text.ParseException)11 Locale (java.util.Locale)10 AttributedString (java.text.AttributedString)9 ArrayList (java.util.ArrayList)9 TemporalAccessor (java.time.temporal.TemporalAccessor)7 Calendar (java.util.Calendar)7