在 Java 中使用日历取消一天?

Take away a single day using Calendar in Java?

我有一个应用程序可以插入 Google 适合 Api 和 returns 最近 7 天的步骤,方法如下。正如屏幕截图所示,我希望将这一天添加到步数中。

我尝试了很多选择,一次带走 7 循环的一天,但没有运气,它只是说同一天。任何帮助都会非常感谢。

private void dumpDataSet(DataSet dataSet) {
    Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
    DateFormat dateFormat = DateFormat.getTimeInstance();

    int i = 0;

    for (DataPoint dp : dataSet.getDataPoints()) {

        for(Field field : dp.getDataType().getFields()) { //loop 7 times

            int test = dp.getValue(field).asInt();

            String weekSteps= String.valueOf(test); //get weekday steps one at a time

            SimpleDateFormat sdf = new SimpleDateFormat("EEEE");



            Calendar cal = Calendar.getInstance();
            String weekday = sdf.format(cal.getTime());

            String weekStepsFinal= weekSteps + " steps on " + weekday; //set Textfield to steps and the day 

            FeedItem item = new FeedItem();
            item.setTitle(weekStepsFinal);

            feedItemList.add(item);

        }
    }

}

顺便说一句,有 7 个数据集。

要减去一天,请使用以下代码:

int DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
Date currentDate = new Date();
long previousDay = currentDate.getTime()-DAY_IN_MILLIS;

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String day = sdf.format(previousDay);

这将从日历中减去 7 天,得到 7 天前的日期:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -7).

如果 "take away one day at a time" 意味着您希望日子倒流,那么方法如下:

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");

System.out.println("Last 7 days (starting today):");
Calendar cal = Calendar.getInstance(); // Initialized to today/now
for (int i = 0; i < 7; i++) {
    System.out.println("  " + sdf.format(cal.getTime()));
    cal.add(Calendar.DAY_OF_MONTH, -1); // Update to previous day at same time-of-day
}

输出

Last 7 days (starting today):
  Monday
  Sunday
  Saturday
  Friday
  Thursday
  Wednesday
  Tuesday