room sqlite查询减去长时间戳

ki0zmccv  于 2021-07-27  发布在  Java
关注(0)|答案(1)|浏览(436)

我有一个表,它有两列:“timestart”和“timefinish”。这些列存储一个长值,该值使用typeclassconverter从日期格式转换而来。
我需要编写一个查询来返回表中的所有行,但我需要计算总时间。因此,我需要减去timestart和timefinish中的long值,以获得每行花费的时间,然后将它们相加。我想返回一个以小时为单位的值,让用户知道他们花了多少小时做活动。我不知道做这件事最好的方法是什么。有人能帮我写一个具有上述功能的查询吗?
table

@Entity(tableName = "tableCheckIn")
public class TableCheckIn {

@PrimaryKey(autoGenerate = true)
private long pkCheckInId;

@ColumnInfo(name = "userId")
private long fkUserId;

@ColumnInfo(name = "moodStart")
private long moodStart;

@ColumnInfo(name = "checkInChallenge")
private String checkInChallenge;

@ColumnInfo(name = "checkInGratitude")
private String checkInGratitude;

@ColumnInfo(name = "workoutStart")
@TypeConverters({TimeTypeConverter.class})
private Date workoutStart;

@ColumnInfo(name = "workoutEnd")
@TypeConverters({TimeTypeConverter.class})
private Date workoutEnd;

日期转换器

public class TimeTypeConverter {

@TypeConverter
public static Long toDate(Date date){
    return date == null ? null : date.getTime();
}

@TypeConverter
public static Date toLong(Long timestamp){
    return timestamp == null ? null : new Date(timestamp);
}

@Query("SELECT (SUM(workoutEnd - workoutStart) / 3600) as totalWorkouts FROM tableCheckIn WHERE userId = :pkUserId")
LiveData<List<ModelStatsTotalWorkoutTime>> totalWorkoutTime(long pkUserId);

回购

public LiveData<List<ModelStatsTotalWorkoutTime>> totalWorkoutTime(long userId) {
    return feelingFitDatabase.getUsersDao().totalWorkoutTime(userId);
}

视图模型

public LiveData<List<ModelStatsTotalWorkoutTime>> totalWorkoutTime(long userId) {
    return repoUsers.totalWorkoutTime(userId);
}

片段

mViewModelUsers.totalWorkoutTime(pkUserId).observe(getViewLifecycleOwner(), new Observer<List<ModelStatsTotalWorkoutTime>>() {
        @Override
        public void onChanged(List<ModelStatsTotalWorkoutTime> modelStatsTotalWorkouts) {

            for(ModelStatsTotalWorkoutTime list: modelStatsTotalWorkouts){
                totalWorkoutTimeSeconds += list.getTotalWorkouts();
            }

            totalWorkoutTimeMinutes = totalWorkoutTimeSeconds / 60;
            totalWorkoutTimeHours = totalWorkoutTimeMinutes / 60;

            if (totalWorkoutTimeMinutes <= 59){
                tvTotalTimeWorkingOut.setText(""+totalWorkoutTimeMinutes);
                tvTotalTimeWorkingOutDescription.setText(getString(R.string.workout_minutes));
            } else {
                tvTotalTimeWorkingOut.setText(""+totalWorkoutTimeHours);
                tvTotalTimeWorkingOutDescription.setText(getString(R.string.workout_hours));
            }
        }
    });
}
jgovgodb

jgovgodb1#

TimeStart 以及 TimeFinish 包含包含毫秒的unix历元时间戳。
在sqlite中,您可以使用以下命令以可读格式获取日期:

select 
  datetime(1596107885085 / 1000, 'unixepoch'),
  datetime(1596108413648 / 1000, 'unixepoch')

返回:

2020-07-30 11:18:05     2020-07-30 11:26:53

要获得它们在分钟内的差异,必须除以1000以去除毫秒,然后再除以60:

(TimeFinish - TimeStart) / 1000 / 60

因此,您可以在几分钟内获得总差异:

select sum(TimeFinish - TimeStart) / 1000 / 60 from tablename

相关问题