Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all the days of the current week in android?

Tags:

android

I want to get all the days in the current week in an array of Strings. How can I acheive this? Thanks in advance.

like image 551
Prachi Avatar asked Sep 16 '11 05:09

Prachi


2 Answers

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.

like image 151
Jon Skeet Avatar answered Mar 25 '23 02:03

Jon Skeet


Calendar c = Calendar.getInstance();
        System.out.println("Current time => " + c.getTime());
        SimpleDateFormat df = new SimpleDateFormat("EEEE");
        formattedDate = df.format(c.getTime());
like image 23
Hiren Patel Avatar answered Mar 25 '23 01:03

Hiren Patel