簡體   English   中英

獲取一周的開始和結束日期

[英]get start and end date of the week

實際上,我想顯示一組數據,其中包含特定日期的一周的開始和結束日期。 在我的模擬器中,它的工作正常。 例如。 如果我給4月23日,它給我一周的開始日期為4月22日,結束日期為4月28日,但是如果我嘗試在我的設備中構建相同的代碼,則它顯示的一周的開始日期為4月27日,結束日期為28四月

我正在使用的一段代碼:

      //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);

我不知道為什么要得到這些數據,但這是我得到第一個和最后一個日期的方式,請看一下可能會有所幫助。 (它的寫法給出了本周的開始和結束日期,因此可能需要稍作調整。)

日歷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);

要獲得一周的第一天-

  1. 根據您的需要初始化日歷。 在這種情況下,我正在獲取當前日期日歷。
  2. 星期幾設置為星期幾。
  3. 得到相應的日期

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

要獲取一周的最后日期-

  1. 請按照上述步驟進行初始化。
  2. 將星期幾設置為星期六。
  3. 獲取相應的日期。

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

通過這種方式,您可以獲取每周的第一和最后一個日期。 請記住,我將一周的第一天設置為SUNDAY。 根據您的需要進行設置。 盡管設置一周的第一天是完全可選的。 這使您的代碼更加透明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM