0

I am using the following code to display all the day of a week, but whenever I select the last date of a week then it prints the date of next week. Why this happens? Is anything wrong with my code?

/**Calendar week start on Monday**/
        
        Calendar selectedDate = Calendar.getInstance(Locale.US);        
        // Set current date to 21-april-2013
        selectedDate.set(selectedDate.get(Calendar.YEAR), selectedDate.get(Calendar.MONTH), 21);
        
        // create another instance of calendar      
        Calendar c = Calendar.getInstance(Locale.US);
        // set current date to this new calendar instance
        c = (Calendar) selectedDate.clone();
                
        // Set the calendar to monday of the current week   
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                
        // Print dates of the current week starting on Monday
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
        for (int i = 0; i < 7; i++) 
        {
            System.out.println(df.format(c.getTime()));
            c.add(Calendar.DATE, 1);
        }

OutPut shows date from

22-april-2013 to 28-april-2013

But it should be

15-april-2013 to 21-april-2013
iknow
  • 8,358
  • 12
  • 41
  • 68
AndroidDev
  • 4,521
  • 24
  • 78
  • 126
  • Your code is working......... Read here for date ranges examples http://pankajchunchun.wordpress.com/2012/06/29/calculate-date-ranges/ – Pankaj Kumar Apr 22 '13 at 09:21
  • your code is working fine for me – Abi Apr 22 '13 at 09:22
  • @Abi Try creating android project in eclipse and run the same code, it shows output from 22-april-2013 to 28-april-2013, yeh its working if u create java project in eclipse – AndroidDev Apr 22 '13 at 09:35
  • Where did you tested the code? on emulator or device... check date settings of using device. – Pankaj Kumar Apr 22 '13 at 09:44
  • I tested it on my device (note, Note2 and galaxy nexus) and in the date setting of my device, automatic date setting is enabled. Is this an android sdk bug, Because the same code works fine when i created and java project and run the project. – AndroidDev Apr 22 '13 at 09:52
  • 1
    http://stackoverflow.com/questions/7299621/android-calendar-problem-with-day-of-the-week – andrew Apr 22 '13 at 10:28

3 Answers3

0

you're adding days to your start date, which is 21 April.

for (int i = 0; i < 7; i++) 
{
    System.out.println(df.format(c.getTime()));
    c.add(Calendar.DATE, 1);
}

This would explain why your dates run into the future =)

mcfinnigan
  • 11,442
  • 35
  • 28
0

You are setting the date as 21 which is sunday..

When you set

c.setFirstDayOfWeek(Calendar.MONDAY);

it will become 15-april-2013

So, c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); clearly set to 15-april-13

Linga
  • 10,379
  • 10
  • 52
  • 104
  • But in my case its not setting the date to 15-april-2013 after doing this c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); instead it set to 22-april-2013 which is wrong, i want it to set to 15-april-2013, and it happen only whenever i selected the last date of a week. But Yeh if u create an java project in eclipse and run my code its work fine, but if u create android project in eclipse and run the same code it shows output from 22-april-2013 to 28-april-2013, and that is the issue – AndroidDev Apr 22 '13 at 09:23
0

Your code is working fine in my system

enter image description here

Soumyadip Das
  • 1,781
  • 3
  • 16
  • 34
  • Yeh if u create an java project in eclipse and run my code its work fine, but if u create android project in eclipse and run the same code it shows output from 22-april-2013 to 28-april-2013, and that is the issue – AndroidDev Apr 22 '13 at 09:24
  • At last u got the issue...why this happen. Do u have any idea for that – AndroidDev Apr 22 '13 at 09:30