在android中获取当前周的日期

时间:2013-10-24 09:51:43

标签: android date

我试图从周日开始获取本周的日期。当前日期是24-11-2013,是星期天。目前的日期是24,所以我喜欢24,25等...但是我得到了前一周。代码有什么问题。

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

它给出了以下几天。

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

1 个答案:

答案 0 :(得分:2)

试试这个

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

输出

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