我正试图从当前的每周和每月的房间中获取数据。我已经创建了一个当前的Day查询,它可以工作,但其他的不起作用。
日查询(这个工作):
@Query("SELECT * FROM expense_table WHERE day = strftime('%d', 'now')")
LiveData<List<Expense>> getExpensesDay();
周:
@Query("SELECT * FROM expense_table WHERE week = strftime('%W', 'now')")
LiveData<List<Expense>> getExpensesWeek();
月:
@Query("SELECT * FROM expense_table WHERE month = strftime('%m', 'now')")
LiveData<List<Expense>> getExpensesMonth();
实体:
@Entity(tableName = "expense_table")
public class Expense {
@PrimaryKey(autoGenerate = true)
private int expenseId;
private String note;
private Double value;
private String type;
private Long dateLong = System.currentTimeMillis();
private String date = new SimpleDateFormat("MM/yyyy").format(new Date(dateLong));
private static Calendar cal = Calendar.getInstance();
private int month = cal.get(Calendar.MONTH);
private int week = cal.get(Calendar.WEEK_OF_YEAR);
private int day = cal.get(Calendar.DAY_OF_MONTH);
private int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
private String weekDay = new DateFormatSymbols().getWeekdays()[dayOfWeek];
根据文件,strftime('%W', ...)
以0为基础代替一年中的一周,而Calendar.get(Calendar.WEEK_OF_YEAR)
以1为基础的年份返回一周。同样地,strftime('%m', ...)
以1为基础替换一年中的一周,而Calendar.get(Calendar.MONTH)
以0为基础返回一年中的一周。
因此,尝试替换Entity
类中的字段计算:
private int month = cal.get(Calendar.MONTH) + 1;
private int week = cal.get(Calendar.WEEK_OF_YEAR) - 1;
代替
private int month = cal.get(Calendar.MONTH);
private int week = cal.get(Calendar.WEEK_OF_YEAR);