使用Java中的Calendar一天带走一天?

时间:2016-03-21 23:46:58

标签: java android calendar google-fit

我有一个应用程序插入Google Fit Api并返回过去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);

        }
    }

}

btw有7个数据集。

enter image description here

3 个答案:

答案 0 :(得分:1)

这将从日历中减去7天,以便在7天前获得日期:

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

答案 1 :(得分:1)

如果通过"一次带走一天"意味着你想要倒退的日子,然后是如何:

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

答案 2 :(得分:0)

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

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);