mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-06 16:56:55 +02:00
set CSV date format to ISO 8601:2004 and implement backward compatible for the old date format dd.MM.yyyy, see issue #506
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.health.openscale.core.datatypes;
|
||||
|
||||
import com.health.openscale.core.utils.CsvHelper;
|
||||
import com.j256.simplecsv.common.CsvColumn;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
@@ -45,8 +46,7 @@ public class ScaleMeasurement implements Cloneable {
|
||||
private int userId;
|
||||
@ColumnInfo(name = "enabled")
|
||||
private boolean enabled;
|
||||
@CsvColumn(format = "dd.MM.yyyy HH:mm", mustNotBeBlank = true)
|
||||
@ColumnInfo(name = "datetime")
|
||||
@CsvColumn(converterClass = CsvHelper.DateTimeConverter.class, format ="yyyy-MM-dd HH:mm", mustNotBeBlank = true) @ColumnInfo(name = "datetime")
|
||||
private Date dateTime;
|
||||
@CsvColumn(mustNotBeBlank = true)
|
||||
@ColumnInfo(name = "weight")
|
||||
|
@@ -17,13 +17,18 @@
|
||||
package com.health.openscale.core.utils;
|
||||
|
||||
import com.health.openscale.core.datatypes.ScaleMeasurement;
|
||||
import com.j256.simplecsv.converter.DateConverter;
|
||||
import com.j256.simplecsv.processor.ColumnInfo;
|
||||
import com.j256.simplecsv.processor.ColumnNameMatcher;
|
||||
import com.j256.simplecsv.processor.CsvProcessor;
|
||||
import com.j256.simplecsv.processor.ParseError;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class CsvHelper {
|
||||
@@ -108,4 +113,22 @@ public class CsvHelper {
|
||||
|
||||
return csvProcessor.readRows(reader, null);
|
||||
}
|
||||
|
||||
// backward compatible for openScale version >= 2.1.2 to support old date format dd.MM.yyyy, see issue #506
|
||||
public static class DateTimeConverter extends DateConverter {
|
||||
private static final SimpleDateFormat srcDateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
|
||||
private static final SimpleDateFormat dstDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
|
||||
@Override
|
||||
public Date stringToJava(String line, int lineNumber, int linePos, ColumnInfo<Date> columnInfo, String value, ParseError parseError) throws ParseException{
|
||||
try {
|
||||
Date srcDate = srcDateFormat.parse(value);
|
||||
value = dstDateFormat.format(srcDate);
|
||||
} catch (ParseException ex) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
return super.stringToJava(line, lineNumber, linePos, columnInfo, value, parseError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user