我想从一个月的一周获得开始日期和结束日期(android)

时间:2017-06-21 10:29:00

标签: android

我想获得一个月特定周的开始日期和结束日期,   例如,如果我从给定日历图片中选择第一周而不是开始日期将是1 / FEB / 2017,结束日期将是4 / FEB / 2017。 感谢。

2 个答案:

答案 0 :(得分:0)

获取用户的日历(例如星期三)的日期,然后运行循环直到您再次出现同一天(直到星期三再次出现)或月末结束。

答案 1 :(得分:0)

日历日历= 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);