【问题标题】:get start and end date of the week获取一周的开始和结束日期
【发布时间】:2018-10-05 12:00:42
【问题描述】:

实际上,我想显示一组数据,其中包含特定日期所在的一周的开始和结束日期。在我的模拟器中它工作正常。例如。如果我给 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);

【问题讨论】:

标签: android android-calendar dayofweek


【解决方案1】:

我不知道您为什么要获取这些数据,但这是我获取第一个和最后一个日期的方式,看看可能会有所帮助。 (它写给本周的第一个和最后一个日期,所以可能需要稍微调整一下。)

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

【讨论】:

  • 是的,谢谢..我以前看过你的帖子并尝试过,但它对我不起作用..!!
【解决方案2】:

获取一周的第一天 -

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

通过这种方式,您可以获得一周的第一个和最后一个日期。需要记住的一件事是,我已将一周的第一天设置为星期日。根据您的需要设置。尽管设置一周的第一天完全是可选的。这使您的代码更加透明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-18
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多