4

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.

elL
  • 767
  • 2
  • 14
  • 35
  • may be use this method calender.get(calender.DAY_OF_WEEK) get the today week – srinivas Apr 13 '13 at 12:59
  • yes, i've also tried that one. it gave me the days of the week but, it started on saturday. The output goes this way: `sat-13 sun-07 mon-08 tues-09 wed-10 thu-11 fri-12` it is very close.. but saturday ruined it.. – elL Apr 13 '13 at 13:14
  • @elL Check my answer.. I have tested, it is working – Pragnani Apr 13 '13 at 13:15

1 Answers1

2

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]);
        }
}
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • hmmm. it starts on Saturday (maybe because today is Saturday??). even though i added this `date.setFirstDayOfWeek(GregorianCalendar.MONDAY);` the result would be `sat-06 sun-07 mon-08 tues-09 wed-10 thur-11 fri-12` ..hmm – elL Apr 13 '13 at 14:09
  • `date.set(GregorianCalendar.DATE, date.get(GregorianCalendar.DATE)-date.get(GregorianCalendar.DAY_OF_WEEK)+1);` add +1 at the end so the out will be dates from `07 08 09 10 11 12 13` and the first day is 7 – Pragnani Apr 13 '13 at 14:13