org.supercsv.cellprocessor.FmtDate Java Examples

The following examples show how to use org.supercsv.cellprocessor.FmtDate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CsvFormatter.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
/**
 * Returns array of cellprocessors, one for each field
 */
private CellProcessor[] getProcessor(List<Field> fields)
{
  CellProcessor[] processor = new CellProcessor[fields.size()];
  int fieldCount = 0;
  for (Field field : fields) {
    if (field.getType() == FieldType.DATE) {
      String format = field.getConstraints().get(DelimitedSchema.DATE_FORMAT) == null ? null
          : (String)field.getConstraints().get(DelimitedSchema.DATE_FORMAT);
      processor[fieldCount++] = new Optional(new FmtDate(format == null ? "dd/MM/yyyy" : format));
    } else {
      processor[fieldCount++] = new Optional();
    }
  }
  return processor;
}
 
Example #2
Source File: Serialization.java    From joinery with GNU General Public License v3.0 6 votes vote down vote up
public static <V> void writeCsv(final DataFrame<V> df, final OutputStream output)
throws IOException {
    try (CsvListWriter writer = new CsvListWriter(new OutputStreamWriter(output), CsvPreference.STANDARD_PREFERENCE)) {
        final String[] header = new String[df.size()];
        final Iterator<Object> it = df.columns().iterator();
        for (int c = 0; c < df.size(); c++) {
            header[c] = String.valueOf(it.hasNext() ? it.next() : c);
        }
        writer.writeHeader(header);
        final CellProcessor[] procs = new CellProcessor[df.size()];
        final List<Class<?>> types = df.types();
        for (int c = 0; c < df.size(); c++) {
            final Class<?> cls = types.get(c);
            if (Date.class.isAssignableFrom(cls)) {
                procs[c] = new ConvertNullTo("", new FmtDate("yyyy-MM-dd'T'HH:mm:ssXXX"));
            } else {
                procs[c] = new ConvertNullTo("");
            }
        }
        for (final List<V> row : df) {
            writer.write(row, procs);
        }
    }
}
 
Example #3
Source File: Writing.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. All values
 * are converted to Strings before writing (there's no need to convert them), and null values will be written as
 * empty columns (no need to convert them to "").
 * 
 * @return the cell processors
 */
private static CellProcessor[] getProcessors() {
	
	final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), // customerNo (must be unique)
		new NotNull(), // firstName
		new NotNull(), // lastName
		new FmtDate("dd/MM/yyyy"), // birthDate
		new NotNull(), // mailingAddress
		new Optional(new FmtBool("Y", "N")), // married
		new Optional(), // numberOfKids
		new NotNull(), // favouriteQuote
		new NotNull(), // email
		new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints
	};
	
	return processors;
}
 
Example #4
Source File: WritingFeaturesTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
@Test
public void testDateSupport() throws IOException {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, 1999);
	calendar.set(Calendar.MONTH, 6);
	calendar.set(Calendar.DAY_OF_MONTH, 12);
	
	FeatureBean character = new FeatureBean("John", "Connor", 16);
	character.setBirthDate(calendar.getTime());
	
	String[] mapping = { "birthDate" };
	DecimalFormat formatter = new DecimalFormat();
	formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance());
	CellProcessor[] processors = { new FmtDate("yyyy-MM-dd") };
	
	StringWriter writer = new StringWriter();
	CsvBeanWriter beanWriter = new CsvBeanWriter(writer, STANDARD_PREFERENCE);
	beanWriter.write(character, mapping, processors);
	beanWriter.close();
	
	String csv = writer.toString();
	Assert.assertNotNull(csv);
	Assert.assertEquals("1999-07-12\r\n", csv);
}
 
Example #5
Source File: CsvResultSetWriterTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * Test that row/line numbers reported during exception are determined correctly
 * 
 * @throws IOException
 * @throws SQLException
 */
@Test(expected = SuperCsvCellProcessorException.class)
public void testRowLineNumberCorrectness() throws SQLException, IOException {
	final int LINE_NUMBER = 5;
	final int ROW_NUMBER = 4;
	final Object[][] causesException = { { "1", "Alexander\r\nGraham", date(1945, 6, 13), },
		{ "2", "Bob", date(1919, 2, 25), }, { "3", "Alice", "CAUSES EXCEPTION", },
		{ "4", "Bill", date(1973, 7, 10), }, { "5", "Miranda", date(1999, 1, 3), }, };
	final String[] headers = { "customerNo", "firstName", "birthDate" };
	final ResultSet resultSet = new ResultSetMock(causesException, headers);
	final CellProcessor[] cellProcessors = { null, null, new FmtDate("dd/MM/yyyy") };
	try {
		csvResultSetWriter.write(resultSet, cellProcessors);
	}
	catch(SuperCsvCellProcessorException e) {
		final int actualLineNumber = e.getCsvContext().getLineNumber();
		final int actualRowNumber = e.getCsvContext().getRowNumber();
		assertEquals("line number not correct", LINE_NUMBER, actualLineNumber);
		assertEquals("row number not correct", ROW_NUMBER, actualRowNumber);
		throw e;
	}
}
 
Example #6
Source File: WritingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertsToBasicObjects() throws IOException {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, 1999);
	calendar.set(Calendar.MONTH, 6);
	calendar.set(Calendar.DAY_OF_MONTH, 12);
	
	FeatureBean character = new FeatureBean("John", "Connor", 16);
	character.setSavings(new BigDecimal(6.65));
	character.setBirthDate(calendar.getTime());
	
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	DecimalFormat formatter = new DecimalFormat();
	formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance());
	CellProcessor[] processors = { new NotNull(), new NotNull(), new NotNull(), new FmtDate("yyyy-MM-dd"),
		new FmtNumber(formatter) };
	
	StringWriter writer = new StringWriter();
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanWriter beanWriter = new CsvBeanWriter(writer, customPreference);
	beanWriter.write(character, mapping, processors);
	beanWriter.close();
	
	String csv = writer.toString();
	Assert.assertNotNull(csv);
	Assert.assertEquals("Connor|John|16|1999-07-12|" + formatter.format(character.getSavings()) + "\r\n", csv);
}
 
Example #7
Source File: WritingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testConverterSupport() throws IOException {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, 1999);
	calendar.set(Calendar.MONTH, 6);
	calendar.set(Calendar.DAY_OF_MONTH, 12);
	
	FeatureBean character = new FeatureBean("John", "Connor", 16);
	character.setSavings(new BigDecimal(6.65));
	character.setBirthDate(calendar.getTime());
	
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	DecimalFormat formatter = new DecimalFormat();
	formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance());
	CellProcessor[] processors = { new NotNull(), new NotNull(), new NotNull(), new FmtDate("yyyy-MM-dd"),
		new FmtNumber(formatter) };
	
	StringWriter writer = new StringWriter();
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanWriter beanWriter = new CsvBeanWriter(writer, customPreference);
	beanWriter.write(character, mapping, processors);
	beanWriter.close();
	
	String csv = writer.toString();
	Assert.assertNotNull(csv);
	Assert.assertEquals("Connor|John|16|1999-07-12|" + formatter.format(character.getSavings()) + "\r\n", csv);
}