简体   繁体   中英

get start and end date of the week

Actually i want to display a set of data with the start and end date of the week were that particular date falls on. In my emulator its working fine. Eg. If i give Apr 23 its giving me start date of the week as 22 Apr and end date as 28 Apr, but if i try to build the same code in my device its showing start date of the week as 27 Apr and end date as 28 Apr.

Piece of Code which i am using:

      //to get first day of week
                    cal1.set(Calendar.DAY_OF_WEEK, 1);
                    int day1 = cal1.get(Calendar.DAY_OF_MONTH);

     //to get last day of week
                    cal1.set(Calendar.DAY_OF_WEEK, 7);
                    int day7 = cal1.get(Calendar.DAY_OF_MONTH);

I don't know why you getting that data but this is how I get the first and last date, take look might help. (its written to give current week's first and last date, so might have to tweak it little bit.)

Calendar calendar = Calendar.getInstance();

Date date1 = calendar.getTime();
//current date to check that our week lies in same month or not
SimpleDateFormat checkformate = new SimpleDateFormat("MM/yyyy");
String currentCheckdate= checkformate.format(date1);

int weekn = calendar.get(Calendar.WEEK_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year  = calendar.get(Calendar.YEAR);
//resat calender without date
calendar.clear();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.WEEK_OF_MONTH,weekn);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.YEAR,year);

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");

Date datef = calendar.getTime();
//move date to 6 days + to get last date of week
Long timeSixDayPlus = calendar.getTimeInMillis()+518400000L;
Date dateL = new Date(timeSixDayPlus);
String firtdate = simpleDateFormat.format(datef);
String lastdate = simpleDateFormat.format(dateL);
String firtdateCheck = checkformate.format(datef);
String lastdateCheck = checkformate.format(dateL);

//if our week lies in two different months then we show only current month week part only
if (!firtdateCheck.toString().equalsIgnoreCase(currentCheckdate)) {
    firtdate = "1" + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
}
if (!lastdateCheck.toString().equalsIgnoreCase(currentCheckdate)) {
    int ma = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    lastdate = String.valueOf(ma) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
}
Log.e("current","=>>"+firtdate+" to "+lastdate);

To get the first day of the week -

  1. initialize the Calendar as per your need. in this case, I am getting the current date calendar.
  2. set the current day of the week to the first day of the week.
  3. get the corresponding date .

     Calendar calendar = Calendar.getInstance(); //optional step calendar.setFirstDayOfWeek(Calendar.SUNDAY); calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); int firstDateOfWeek = calendar.get(Calendar.DATE); 

To get the last date of the week -

  1. follow the step as above to initialize.
  2. set the day of the week as Saturday.
  3. get the corresponding date.

     Calendar calendar = Calendar.getInstance(); //optional step calendar.setFirstDayOfWeek(Calendar.SUNDAY); calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); int lastDateOfWeek = calendar.get(Calendar.DATE); 

In this way, you can get eh first and the last date for the week. one thing to keep in mind that I have set the first day of the week as SUNDAY. set as per your need. although it is purely optional to set the first day of the week. this gives you a more transparency in code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM