我想在房间数据库中使用日期类型作为ID,主要原因是能够检查此日期是否是今天。任何建议都是赞赏
@Entity
data class DailyCount(@PrimaryKey
var date:DateTime,// JodaTime
var summ: MutableLiveData<Double>? = MutableLiveData())
我想这样进行查询:
@Query("update DailyCount set summ = :sum where date = :dailyCount") //update if date is today
fun apdateCash(dailyCount: DateTime, sum: Double)
只需使用simperedateformatter
String jsonDateStr = "03-09-2019 05:45:10"
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy");
try {
return fmt.parse(jsonDateStr);
} catch(ParseException pe) {
return //generate different unique ID like GUID random maybe;
}
现在,如果您首先有日期对象,并且需要将其归零,则可以这样做:
Date dateObject = Date("03-09-2019 05:45:10") //pseudo for visual
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy");
try {
String dateWithZeroedTime = fmt.format(dateObject)
return fmt.parse(dateWithZeroedTime) //"03-09-2019 00:00:00"
} catch(ParseException pe) {
return //generate different unique ID like GUID random maybe;
}
快乐编码!