简体   繁体   中英

Get previous and next week days

I'm able to list current week dates . But How to list previous / next week days ?

( I want to use a button to list previous and next week days. Whenever I click it will show one week before/ after )

Calendar cal = Calendar.getInstance(Locale.UK);     
        cal.set(Calendar.DAY_OF_WEEK,  cal.getFirstDayOfWeek());        
        daydate = new SimpleDateFormat("dd.MM.yyyy");       

        for (int i = 0; i < 7; i++) {   
             dates[i] = daydate.format(cal.getTime());     
            cal.add(Calendar.DAY_OF_WEEK, 1);
}

To display next week follow below code.

    List next = new ArrayList();
    for (int i = 0; i < 7; i++) {
    c.add(Calendar.DATE, 1);
    start = df.format(c.getTime());
    next.add(start);
    }
   textview.setText("" + next.get(0) + "-" + next.get(6));

To display previous week follow the below code:

    List previous = new ArrayList();
            for (int i = 0; i < 7; i++) {
                c.add(Calendar.DATE, -1);
                start = df.format(c.getTime());
                previous.add(start);
            }
            textview.setText("" + previous.get(6) + "-" + previous.get(0));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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