简体   繁体   中英

Getting dates of the current week in android

I am trying to get the dates of the current week, starting from Sunday. The current date is 24-11-2013, and it is Sunday. The current date is 24, so I like to have 24,25 etc... But I get the previous week. What is wrong in the code.

String[] days = new String[7];
DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setFirstDayOfWeek(Calendar.SUNDAY);
            calendar1.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            for (int i = 0; i < 7; i++) {
                days[i] = format.format(calendar1.getTime());
                calendar1.add(Calendar.DAY_OF_MONTH, 1);
                Log.v("datessssssssss", days[i]);
            }

It gives the following days.

11-24 15:16:41.324: V/datessssssssss(12256): 17-11-2013
    11-24 15:16:41.324: V/datessssssssss(12256): 18-11-2013
    11-24 15:16:41.324: V/datessssssssss(12256): 19-11-2013
    11-24 15:16:41.324: V/datessssssssss(12256): 20-11-2013
    11-24 15:16:41.324: V/datessssssssss(12256): 21-11-2013
    11-24 15:16:41.324: V/datessssssssss(12256): 22-11-2013
    11-24 15:16:41.324: V/datessssssssss(12256): 23-11-2013

Try this

    // Get calendar set to current date and time
        Calendar c = Calendar.getInstance();

        // Set the calendar to Sunday of the current week
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        // Print dates of the current week starting on Sunday
        DateFormat df = new SimpleDateFormat("EEE dd/MM/yyyy");
        for (int i = 0; i < 7; i++) {
            System.out.println(df.format(c.getTime()));
            c.add(Calendar.DATE, 1);
        }

output is

10-24 15:35:17.070: I/System.out(26150): Sun 20/10/2013
10-24 15:35:17.070: I/System.out(26150): Mon 21/10/2013
10-24 15:35:17.080: I/System.out(26150): Tue 22/10/2013
10-24 15:35:17.080: I/System.out(26150): Wed 23/10/2013
10-24 15:35:17.080: I/System.out(26150): Thu 24/10/2013
10-24 15:35:17.080: I/System.out(26150): Fri 25/10/2013
10-24 15:35:17.080: I/System.out(26150): Sat 26/10/2013

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