Question

i want to display the days of the week with the date of that week. Like today, i want to display for my calendar is like this:

Mon  Tues  Wed  Thurs  Fri  Sat   Sun
8    9     10    11    12   13    14 

Then, when it another week starts, the numbers changes.. So, i experimented with the android calendar class. So i came out with this:

TextView[] tx = {t1, t2, t3, t4, t5, t6, t7};

    SimpleDateFormat curFormater = new SimpleDateFormat("EEE dd"); 
    GregorianCalendar date = new GregorianCalendar();
    String[] dateStringArray = new String[7];

    for (int day = 0; day < 7; day++) {
        dateStringArray[day] = curFormater.format(date.getTime());
        date.roll(Calendar.DAY_OF_MONTH, true);
        System.out.println("HELLO WORLD DAYS: " + dateStringArray[day]);
        tx[day].setText(dateStringArray[day]);
    }

just to check if i can attain my objective. Well, it is close to what i want. The problem is, for example today is Saturday, the output is this:

Sat   Sun   Mon   Tue   Wed   Thur   Fri
13    14    15    16    17    18     19

Then, tomorrow..i'm guessing this would become

Sun   Mon   Tue   Wed   Thur   Fri  Sat
14    15    16    17    18     19   20

So, the starting day changes. Is there a method/s that will help me attain my objective. Maybe i've missed that out while reading the document. Please Thanks. Any help would be appreciated.

Was it helpful?

Solution

Try this

   SimpleDateFormat curFormater = new SimpleDateFormat("EEE dd"); 
        GregorianCalendar date = new GregorianCalendar();
        String[] dateStringArray = new String[7];
             date.set(GregorianCalendar.DATE, date.get(GregorianCalendar.DATE)-date.get(GregorianCalendar.DAY_OF_WEEK));
        for (int day = 0; day < 7; day++) {
            dateStringArray[day] = curFormater.format(date.getTime());
            date.setFirstDayOfWeek(day);
            date.roll(Calendar.DAY_OF_MONTH, true);
            System.out.println("HELLO WORLD DAYS: " + dateStringArray[day]);
        }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top