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"));
}
}
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());
}
}
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());
}
}
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());
}
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;
}
Aggregations