简体   繁体   English

Android获取上周日期

[英]Android get previous week dates

I have some code that gets the current week and loads it into an array: 我有一些获取当前星期并将其加载到数组中的代码:

DateFormat format = new SimpleDateFormat("M-dd");
calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

days = new String[7];
for (int i = 0; i < 7; i++)
{
calendar.add(Calendar.DATE, 1);
days[i] = format.format(calendar.getTime());
}

I then have some code that runs on the selection of a button that will retrieve the previous week dates and loads them into an array: 然后,我在选择按钮的按钮上运行了一些代码,该按钮将检索前一周的日期并将其加载到数组中:

DateFormat format = new SimpleDateFormat("M-dd");

days = new String[7];

for (int i = 0; i < 7; i++)
{
calendar.add(Calendar.DATE, -1);
days[i]=format.format(calendar.getTime());
}

My issue is that it will load for example: 5-22-17, 5-23-17, 5-24-17, 5-25-17, 5-26-17, 5-27-17, 5-28-17 into the initial load for the first week and then when I click on my Previous button to run the previous button code it only goes back 1 day: 5-21-17 - 5-27-17. 我的问题是,它将加载例如:5-22-17、5-23-17、5-24-17、5-25-17、5-26-17、5-27-17、5-28- 17进入第一周的初始负载,然后当我单击我的Previous按钮运行前一个按钮代码时,它只能返回1天:5-21-17-5-27-17。 If I click it again it then goes back a whole week: 5-14-17 - 5-20-17. 如果我再次单击它,则返回整个一周:5-14-17-5-20-17。

How can I fix my code to retrieve the previous week days correctly on the first click? 如何修正我的代码,以便在第一次点击时正确地检索到以前的工作日?

I believe the issue is the fact the calendar is advancing and retreating, rather than being used as an anchor. 我认为问题在于calendar在前进和后退,而不是被用作锚。 This code shows one approach to handling the situation. 此代码显示了一种处理这种情况的方法。 It keeps the current start of the week, and obtains days from that point. 它保持当前的星期开始,并从该点开始获取天数。

/**
 * Returns 7 days starting from the specified starting date
 */
private static String[] getDays(Calendar starting)
{
    DateFormat format = new SimpleDateFormat("M-dd");
    String[] days = new String[7];
    Calendar mod = Calendar.getInstance();
    mod.setTime(starting.getTime());

    for (int i = 0; i < days.length; ++i) {
        mod.add(Calendar.DATE, 1);
        days[i] = format.format(mod.getTime());
    }

    return days;
}

public static void main(String[] args)
{
    // this sets to the beginning of the current week
    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(Calendar.SUNDAY);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

    // need this to back up
    Calendar beginningOfWeek = cal;

    // generate the array
    String days[] = getDays(beginningOfWeek);

    System.out.println(Arrays.toString(getDays(beginningOfWeek)));

    // go forward one week
    beginningOfWeek.add(Calendar.DATE, 7);
    System.out.println(Arrays.toString(getDays(beginningOfWeek)));


    // go back one week; same as starting
    beginningOfWeek.add(Calendar.DATE, -7);
    System.out.println(Arrays.toString(getDays(beginningOfWeek)));

    // go back one week; one week before we began
    beginningOfWeek.add(Calendar.DATE, -7);
    System.out.println(Arrays.toString(getDays(beginningOfWeek)));

}

Example output: 输出示例:

[5-22, 5-23, 5-24, 5-25, 5-26, 5-27, 5-28] [5-22、5-23、5-24、5-25、5-26、5-27、5-28]
[5-29, 5-30, 5-31, 6-01, 6-02, 6-03, 6-04] [5-29、5-30、5-31、6-01、6-02、6-03、6-04]
[5-22, 5-23, 5-24, 5-25, 5-26, 5-27, 5-28] [5-22、5-23、5-24、5-25、5-26、5-27、5-28]
[5-15, 5-16, 5-17, 5-18, 5-19, 5-20, 5-21] [5-15、5-16、5-17、5-18、5-19、5-20、5-21]

Note there are several optimizations that could be applied, such as not creating the format every time the getDays(...) method is called. 请注意,可以应用多种优化方法,例如,每次getDays(...)方法时都不创建format Also, the variable names are essentially from the OP, but are perhaps not optimal. 同样,变量名称本质上来自OP,但可能不是最佳名称。

First, you set first day of week to Sunday, but you then add 1 before getting first value, so result is Mon 5/22 to Sun 5/28 , not Sun 5/21 to Sat 5/27 . 首先,将一周的第一天设置为星期日,但是获得第一个值之前 ,您应加1,因此结果是Mon 5/22Sun 5/21 Sun 5/28 ,而不是Sun 5/21Sat 5/27

Second, when your first loop is done, the Calendar object is sitting at 5/28 . 其次,当您完成第一个循环时,Calendar对象位于5/28 You then go backwards, subtracting 1 before getting last value, so result is 5/27 down to 5/21 , not 5/21 to 5/27 like you said. 然后,您向后移动, 获得最后一个值之前减去1,因此结果是5/27 降至 5/21 ,而不是像您所说的5/215/27

That leaves Calendar object sitting at 5/21 . 这样日历对象就位于5/21 Repeating the above, you get 5/20 down to 5/14 , not 5/14 to 5/20 . 重复上述操作,您将5/20降低到5/14 ,而不是5/145/20

So, to get your results as a week of Sunday to Saturday, like you told the Calendar that you wanted, add 1 after the getting the value. 因此,要在星期天至星期六的一周中获得结果,就像您告诉日历所要的那样,请在获取值加1。 And to get a week in the right order, always get them incrementally. 为了以正确的顺序获得一周的时间,请始终以递增的方式获取它们。 To go back to previous week, since Calendar is sitting after the week, you go back 2 weeks to start building the previous week. 要返回上一周,由于日历是该周之后放置的,因此请返回2周以开始构建上周。

public class Test {
    private Calendar calendar;
    public String[] getCurrentWeek() {
        this.calendar = Calendar.getInstance();
        this.calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        this.calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return getNextWeek();
    }
    public String[] getNextWeek() {
        DateFormat format = new SimpleDateFormat("M-dd");
        String[] days = new String[7];
        for (int i = 0; i < 7; i++) {
            days[i] = format.format(this.calendar.getTime());
            this.calendar.add(Calendar.DATE, 1);
        }
        return days;
    }
    public String[] getPreviousWeek() {
        this.calendar.add(Calendar.DATE, -14);
        return getNextWeek();
    }
    public static void main(String[] args) {
        Test t = new Test();
        System.out.println("Current : " + Arrays.toString(t.getCurrentWeek()));
        System.out.println("Previous: " + Arrays.toString(t.getPreviousWeek()));
        System.out.println("Previous: " + Arrays.toString(t.getPreviousWeek()));
        System.out.println("Next    : " + Arrays.toString(t.getNextWeek()));
        System.out.println("Next    : " + Arrays.toString(t.getNextWeek()));
    }
}

Output 产量

Current : [5-21, 5-22, 5-23, 5-24, 5-25, 5-26, 5-27]
Previous: [5-14, 5-15, 5-16, 5-17, 5-18, 5-19, 5-20]
Previous: [5-07, 5-08, 5-09, 5-10, 5-11, 5-12, 5-13]
Next    : [5-14, 5-15, 5-16, 5-17, 5-18, 5-19, 5-20]
Next    : [5-21, 5-22, 5-23, 5-24, 5-25, 5-26, 5-27]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM