当应用程序的默认语言环境为“ ar”阿拉伯语且设备语言为阿拉伯语时,使用Ormlite保存日期时出现问题。日期用阿拉伯数字保存在SQLite中,但是我需要Ormlite来保存带有英语区域设置的数据。
我尝试使用这个persisterClass,但不能解决我的问题:
public class DateStringSQLiteType extends DateStringType {
private static SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
protected static final DateStringFormatConfig dateFormatConfig = new DateStringFormatConfig(
sdfDateTime.toPattern());
private static final DateStringSQLiteType singleTon = new DateStringSQLiteType();
public static DateStringSQLiteType getSingleton() {
return singleTon;
}
private DateStringSQLiteType() {
super(SqlType.STRING, new Class<?>[0]);
}
/**
* Convert a default string object and return the appropriate argument to a
* SQL insert or update statement.
*/
@Override
public Object parseDefaultString(FieldType fieldType, String defaultStr)
throws SQLException {
DateStringFormatConfig formatConfig = convertDateStringConfig(
fieldType, dateFormatConfig);
try {
// we parse to make sure it works and then format it again
return normalizeDateString(formatConfig, defaultStr);
} catch (ParseException e) {
throw SqlExceptionUtil.create("Problems with field " + fieldType
+ " parsing default date-string '" + defaultStr
+ "' using '" + formatConfig + "'", e);
}
}
/**
* Return the SQL argument object extracted from the results associated with
* column in position columnPos. For example, if the type is a date-long
* then this will return a long value or null.
*
* @param fieldType Associated FieldType which may be null.
* @throws SQLException If there is a problem accessing the results data.
*/
@Override
public Object resultToSqlArg(FieldType fieldType, DatabaseResults results,
int columnPos) throws SQLException {
return results.getString(columnPos);
}
/**
* Return the object converted from the SQL arg to java. This takes the
* database representation and converts it into a Java object. For example,
* if the type is a date-long then this will take a long which is stored in
* the database and return a Date.
*
* @param fieldType Associated FieldType which may be null.
* @param sqlArg SQL argument converted with
* {@link #resultToSqlArg(FieldType, DatabaseResults, int)} which
* will not be null.
*/
@Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos)
throws SQLException {
String value = (String) sqlArg;
DateStringFormatConfig formatConfig = convertDateStringConfig(
fieldType, dateFormatConfig);
try {
return parseDateString(formatConfig, value);
} catch (ParseException e) {
throw SqlExceptionUtil.create("Problems with column " + columnPos
+ " parsing date-string '" + value + "' using '"
+ formatConfig + "'", e);
}
}
/**
* Convert a Java object and return the appropriate argument to a SQL insert
* or update statement.
*/
@Override
public Object javaToSqlArg(FieldType fieldType, Object obj) {
DateFormat dateFormat = convertDateStringConfig(fieldType,
dateFormatConfig).getDateFormat();
return dateFormat.format((Date) obj);
}
/**
* @throws SQLException If there are problems creating the config object. Needed for
* subclasses.
*/
@Override
public Object makeConfigObject(FieldType fieldType) {
String format = fieldType.getFormat();
if (format == null) {
return dateFormatConfig;
} else {
return new DateStringFormatConfig(format);
}
}
}
答案 0 :(得分:0)
另一种将日期时间转换为时间戳值,然后另存为String或Double ...