2013-04-07 157 views
0

我正在尋找一種方法來將DateTime對象和Duration作爲我的值對象的一部分。在Android上使用JodaTime(日期時間,持續時間)和ORMLite

這裏是我的價值對象:

@DatabaseTable(tableName = DrivingRecord.TableName) 
public class DrivingRecord { 
    public final static String TableName = "drivingRecord"; 
    public final static String DRIVING_TASK_COLUMN_NAME = "drivingTask"; 

    @DatabaseField(foreign = true, columnName = DRIVING_TASK_COLUMN_NAME) 
    private DrivingTask drivingTask; 

    @DatabaseField(generatedId = true) 
    private int id; 

    @DatabaseField(dataType = DataType.LONG) 
    private DateTime startTime; 

    @DatabaseField 
    private Duration durationOfDriving; 
} 

我得到以下異常:

java.lang.IllegalArgumentException: Field class org.joda.time.Duration for field FieldType:name=durationOfDriving,class=DrivingRecord is not valid for type [email protected], maybe should be long 

我也越來越想創建的日期時間的條目

回答

2
java.lang.IllegalArgumentException: Field class org.joda.time.Duration for 
    field FieldType:name=durationOfDriving,class=DrivingRecord is not valid for 
    type [email protected], maybe should be long 

此消息是想告訴你,你的DurationdurationOfDriving不與日式DataType.LONG兼容。我不知道,如果你已經發布的代碼是不正確的,但我本來以爲你會嘗試做:

@DatabaseField(dataType = DataType.LONG) 
private Duration durationOfDriving; 

ORMLite原生支持DateTime但不支持持續一個Duration。只是試圖迫使它很長時間不會工作。您將不得不爲Duration定義客戶持有人。

請查看documentation on custom persisters瞭解如何開始自己的持卡人。

您也可以看看這個答案又如:Is there any way to disable ORMLite's check that a field declared with DataType.SERIALIZABLE implements Serializable?

+0

怎麼樣LocalDateTime?我必須爲此編寫一個自定義的持久存儲器嗎? – simonides 2014-10-19 00:31:20

1

這裏的時候,同樣的異常是顯示我能解決問題。使時間戳變長,並保存時區。現在,如果需要,我可以在時間戳上進行搜索。

@DatabaseTable(tableName = DrivingRecord.TableName) 
public class DrivingRecord { 
    public final static String TableName = "drivingRecord"; 
    public final static String DRIVING_TASK_COLUMN_NAME = "drivingTask"; 

    @DatabaseField(foreign = true, columnName = DRIVING_TASK_COLUMN_NAME) 
    private DrivingTask drivingTask; 

    @DatabaseField(generatedId = true) 
    private int id; 

    @DatabaseField(dataType = DataType.LONG) 
    private long startTime; 

    @DatabaseField 
    private Duration durationOfDriving; 

    @DatabaseField(canBeNull = false, dataType= DataType.SERIALIZABLE) 
    private DateTimeZone timeZone; 

    public void setDateTime(DateTime dateTime){ 
     this.startTime = startTime.getMillis(); 
     this.timeZone = startTime.getZone(); 
    } 

    public DateTime getDateTime(){ 
     return new DateTime(startTime, timeZone); 
    } 

} 
0

雖然JodaTime不ORMLite工作,你可以使用Java日期與ORMLite工作。 您還可以在JodaTime和Date之間轉換許多方法。

@DatabaseField 
protected Date createdAt; 

public static String formatDateTime(DateTime dateTime1) { 
    if (dateTime1 == null || dateTime1.getYear() == 0) { 
     return ""; 
    } 
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, ApplicationBase.AppContext.getResources().getConfiguration().locale); 
    Calendar calendar02 = new GregorianCalendar(dateTime1.getYear(), dateTime1.getMonthOfYear() - 1, dateTime1.getDayOfMonth()); 
    Date date3 = calendar02.getTime(); 
    String date3str = DataItemBase.formatDate(date3); 
    return date3str; 
} 
相關問題