简体   繁体   English

获取一周的开始和结束日期

[英]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. 如果我给4月23日,它给我一周的开始日期为4月22日,结束日期为4月28日,但是如果我尝试在我的设备中构建相同的代码,则它显示的一周的开始日期为4月27日,结束日期为28四月

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(); 日历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. 请记住,我将一周的第一天设置为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. 这使您的代码更加透明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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