0

I'm trying to get start and end dates of current week, start day must be the SUNDAY and end Day must be the SATURDAY, I have written the following code ---

Calendar cal1 = Calendar.getInstance();
// Set start Day as SUNDAY
cal1.setFirstDayOfWeek(Calendar.SUNDAY);

cal1.set(Calendar.DAY_OF_WEEK, cal1.getFirstDayOfWeek());

Date fromDate=cal1.getTime();
String strFromDate = df.format(fromDate); 

cal1.add(Calendar.DAY_OF_YEAR, 6);
Date toDate=cal1.getTime();
String strToDate = df.format(toDate);

Above code working excellent on emulator and other devices except "SAMSUNG device". In Samsung device, it gives start and end dates of next week for the same code. Please help.. Thanks..!!

Dnyanesh M
  • 1,349
  • 4
  • 18
  • 46
  • 1
    `SAMSUNG` is fine, what is soft version runs on it? 2.x? – Maxim Shoustin Dec 04 '13 at 09:02
  • I have test it on 4.0 and 4.1 version, but this is the problem only with SAMSUNG device.. – Dnyanesh M Dec 04 '13 at 09:16
  • Because I also have test it on Sony, Carbon, Croma And Samsung devices all having version 4.1, but getting different behavior on Samsung device only..! – Dnyanesh M Dec 04 '13 at 09:24
  • I would recommend not to use `set` with `DAY_OF_WEEK` as it sometimes produces undesired behavior. You'd better subtract necessary number of days and be 100% sure in your code. You may also check here: http://stackoverflow.com/questions/1319473/java-calendar-setcalendar-day-of-week-calendar-sunday-will-it-roll-backwards – Alexey A. Dec 04 '13 at 09:33
  • @AlexeyA. You are right. Problem solved. Here I comes to know that, SAMSUNG do not supports to calendar.setFirstDayOfWeek(Calendar.SUNDAY); here we need to do manual arithmetic. Thanks to AlexeyA. – Dnyanesh M Dec 04 '13 at 11:16

1 Answers1

0

Have you tries to use the Time class instead? (android.text.format.Time) Since Time is an android class, I would expect it to give the same result on every platform

Basile Perrenoud
  • 4,039
  • 3
  • 29
  • 52