I want to get all the days in the current week in an array of Strings. How can I acheive this? Thanks in advance.
Asked
Active
Viewed 1.1k times
7
-
It's not really clear what you're after. What would the contents of those strings be, exactly? If you could give an example, it would really help. – Jon Skeet Sep 16 '11 at 06:00
-
I want to get all the days of the current week like today is 09/16/2011, So I want to get all the days of this week... ex- Str[0]= 09/12/2011 Str[1]= 09/13/2011 Str[2]= 09/14/2011 Str[3]= 09/15/2011 Str[4]= 09/16/2011 Str[5]= 09/17/2011 Str[6]= 09/18/2011 – Prachi Sep 16 '11 at 06:10
-
Right - and *always* in that format, or in the user's default date format? (For example, 09/12/2011 looks like the 9th of December to me, as I'm in the UK.) – Jon Skeet Sep 16 '11 at 06:12
-
It should be in mm/dd/yyyy format. – Prachi Sep 16 '11 at 06:12
-
What do you mean? You need to *specify* the format, or we can't answer you... – Jon Skeet Sep 16 '11 at 06:13
-
It should be in mm/dd/yyyy format. – Prachi Sep 16 '11 at 06:21
2 Answers
27
I think you want something like this... assuming you always want weeks starting on Monday, and a date format of "MM/dd/yyyy".
DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String[] days = new String[7];
for (int i = 0; i < 7; i++)
{
days[i] = format.format(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
While I think that's correct, I personally use Joda Time wherever possible - I'm aware that's slightly trickier on Android where you're more likely to have space concerns.

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
Hello, this is work for me but i am getting issue in some Samsung and some Moto devices, before calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); it will return correct date but when i set calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); and get current week with your code it will give me previous Week. Can you help me.. – Umesh AHIR Apr 20 '18 at 07:10
-
@UmeshAHIR: Not from just that description, I'm afraid. Please ask a new question with more details, ideally a [mcve]. – Jon Skeet Apr 20 '18 at 07:26
-
Hello Jon Skeet i have create question, can you help me? https://stackoverflow.com/questions/49939067/getting-issues-on-get-current-week-days-on-some-device – Umesh AHIR Apr 20 '18 at 10:02
-
-
1
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df = new SimpleDateFormat("EEEE");
formattedDate = df.format(c.getTime());

Hiren Patel
- 52,124
- 21
- 173
- 151