Search in sources :

Example 6 with DateFormatProperty

use of org.simpleflatmapper.map.property.DateFormatProperty in project SimpleFlatMapper by arnaudroger.

the class CsvWriterTest method testWriterWithFormatter.

@Test
public void testWriterWithFormatter() throws ParseException, IOException {
    StringWriter sw = new StringWriter();
    CsvWriter.from(DbObject.class).columns("id", "name").column("creation_time", new DateFormatProperty("dd/MM/yyyy")).column("type_ordinal", new EnumOrdinalFormatProperty()).to(sw).append(newDbObject());
    assertEquals("id,name,creation_time,type_ordinal\r\n" + "13,name,06/06/2015,1\r\n", sw.toString());
}
Also used : StringWriter(java.io.StringWriter) DbObject(org.simpleflatmapper.test.beans.DbObject) EnumOrdinalFormatProperty(org.simpleflatmapper.map.property.EnumOrdinalFormatProperty) DateFormatProperty(org.simpleflatmapper.map.property.DateFormatProperty) Test(org.junit.Test)

Example 7 with DateFormatProperty

use of org.simpleflatmapper.map.property.DateFormatProperty in project SimpleFlatMapper by arnaudroger.

the class CsvMapperDateFormatTest method testReadMultipleFormatOverrideDefault.

@Test
public void testReadMultipleFormatOverrideDefault() throws Exception {
    String format1 = "dd/MM/yyyy";
    String format2 = "MM-dd-yyyy";
    CsvMapper<ObjectWithDate> mapper = CsvMapperFactory.newInstance().defaultDateFormat(format1).addColumnProperty(TRUE, new DateFormatProperty(format2)).newMapper(ObjectWithDate.class);
    String data1 = "date1\n18/06/2016";
    String data2 = "date1\n06-19-2016";
    try {
        mapper.forEach(new StringReader(data1), new ListCollector<ObjectWithDate>()).getList();
        fail();
    } catch (Exception e) {
    // 
    }
    List<ObjectWithDate> list = mapper.forEach(new StringReader(data2), new ListCollector<ObjectWithDate>()).getList();
    assertEquals(1, list.size());
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    assertEquals(sdf.parse("20160619"), list.get(0).date1);
}
Also used : ListCollector(org.simpleflatmapper.util.ListCollector) StringReader(java.io.StringReader) SimpleDateFormat(java.text.SimpleDateFormat) MappingException(org.simpleflatmapper.map.MappingException) IOException(java.io.IOException) ParseException(java.text.ParseException) DateFormatProperty(org.simpleflatmapper.map.property.DateFormatProperty) Test(org.junit.Test)

Example 8 with DateFormatProperty

use of org.simpleflatmapper.map.property.DateFormatProperty in project SimpleFlatMapper by arnaudroger.

the class FieldMapperToAppendableFactoryTest method testJodaDateTimeWithDateFormat.

@Test
public void testJodaDateTimeWithDateFormat() throws Exception {
    MappingContextFactoryBuilder<JodaObject, CsvColumnKey> builder = getMappingContextBuilder();
    FieldMapperColumnDefinition<CsvColumnKey> format = FieldMapperColumnDefinition.<CsvColumnKey>identity().add(new DateFormatProperty("yyyyMMdd"));
    FieldMapper<JodaObject, Appendable> fieldMapper = defaultFieldAppenderFactory.newFieldMapper(newPropertyMapping("dateTime", jodaObjectClassMeta, format), builder, null);
    testFieldMapper("20140607", fieldMapper, jodaObject, builder.newFactory());
}
Also used : CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) DateFormatProperty(org.simpleflatmapper.map.property.DateFormatProperty) Test(org.junit.Test)

Aggregations

DateFormatProperty (org.simpleflatmapper.map.property.DateFormatProperty)8 Test (org.junit.Test)6 SimpleDateFormat (java.text.SimpleDateFormat)4 StringReader (java.io.StringReader)3 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)3 EnumOrdinalFormatProperty (org.simpleflatmapper.map.property.EnumOrdinalFormatProperty)3 DbObject (org.simpleflatmapper.test.beans.DbObject)2 ListCollector (org.simpleflatmapper.util.ListCollector)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Type (java.lang.reflect.Type)1 Format (java.text.Format)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1 MappingException (org.simpleflatmapper.map.MappingException)1 BooleanFieldMapper (org.simpleflatmapper.map.fieldmapper.BooleanFieldMapper)1 CharacterFieldMapper (org.simpleflatmapper.map.fieldmapper.CharacterFieldMapper)1 FieldMapperImpl (org.simpleflatmapper.map.fieldmapper.FieldMapperImpl)1 FloatFieldMapper (org.simpleflatmapper.map.fieldmapper.FloatFieldMapper)1 IntFieldMapper (org.simpleflatmapper.map.fieldmapper.IntFieldMapper)1