【发布时间】:2018-08-14 14:51:13
【问题描述】:
我的 TypeConverter 无法将 GregorianCalendar 转换为 sql Date 类型。以下代码出现错误:
/** due date of item */
@TypeConverters({CalendarConverter.class})
private GregorianCalendar dueDate = null;
错误:(42, 31) 错误:无法弄清楚如何将此字段保存到数据库中。您可以考虑为其添加类型转换器。
TypeConverter 代码:
public class CalendarConverter {
@TypeConverter
public static GregorianCalendar fromDate(Date value){
if(value == null){
throw new NullPointerException("must not be null");
}
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(value);
return cal;
}
@TypeConverter
public static Date toDate(GregorianCalendar calendar){
if(calendar != null){
throw new NullPointerException("must not be null");
}
Calendar cal = calendar;
return new Date(cal.getTime().getTime());
}
}
【问题讨论】:
标签: java android-studio android-room date-conversion java.util.calendar