Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current week days with dates

I want to get current week dates

Lets think: today is Tuesday 07.05.2013. I want get a list of this week days with dates

How can I do this ?

Sunday 05.05.2013
Monday 06.05.2013
*Tuesday 07.05.2013
Wednesday 08.05.2013
Thursday 09.05.2013
Friday 10.05.2013
Saturday 11.05.2013
like image 226
metemet06 Avatar asked May 05 '13 19:05

metemet06


2 Answers

This code will work using system first day of week, that might be different from Sunday.

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE dd.MM.yyyy");

    for (int i = 0; i < 7; i++) {
        Log.i("dateTag", sdf.format(cal.getTime()));
        cal.add(Calendar.DAY_OF_WEEK, 1);
    }
like image 158
andrew Avatar answered Nov 14 '22 23:11

andrew


Try this ->

Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, 5);
c.set(Calendar.MONTH, 7);
c.set(Calendar.YEAR, 2013);

int weekNo = c.get(Calendar.WEEK_OF_YEAR);
c.set(Calendar.WEEK_OF_YEAR, weekNo);

c.clear();

c.set(Calendar.WEEK_OF_YEAR, weekNo);
c.set(Calendar.YEAR, 2013);


SimpleDateFormat formatter = new SimpleDateFormat("EEE dd/MM/yyyy"); 
Date startDate = c.getTime();
c.add(Calendar.DATE, 1);
for (int i = 0; i < 5; i++) {
    Log.d(formatter.format(c.getTime()));
    c.add(Calendar.DATE, 1);
}
like image 34
Vishal Pawale Avatar answered Nov 15 '22 00:11

Vishal Pawale