Android当前周的开始和结束日期

Sim*_*neB 3 android calendar

我需要在Android中填写当周的开始和结束日.

例子

today: Oct 12 2011 -> result: Oct 10 2011 - Oct 16 2011

today: Oct 1 2001 -> result: Sep 26 2011 - Oct 2 2011

today: Dec 30 2011 -> result: Dec 24 2001 - Jan 1 2011
Run Code Online (Sandbox Code Playgroud)

使用c.get(Calendar.WEEK_OF_YEAR);我可以得到周数,但如何获得开始和结束数据?我在这里找到了一个答案MonthDisplayHelper但是,如何使用它?

谢谢!

Sim*_*neB 13

使用这个sintax并且它起作用了

    Calendar c1 = Calendar.getInstance();

    //first day of week
    c1.set(Calendar.DAY_OF_WEEK, 1);

    int year1 = c1.get(Calendar.YEAR);
    int month1 = c1.get(Calendar.MONTH)+1;
    int day1 = c1.get(Calendar.DAY_OF_MONTH);

    //last day of week
    c1.set(Calendar.DAY_OF_WEEK, 7);

    int year7 = c1.get(Calendar.YEAR);
    int month7 = c1.get(Calendar.MONTH)+1;
    int day7 = c1.get(Calendar.DAY_OF_MONTH);  
Run Code Online (Sandbox Code Playgroud)


Tar*_*ara 6

这是一个很好的示例代码,它为您提供一年中的当前一周以及星期的开始和结束日期,您只需要在代码中设置一周的开始日期,就我而言,我将其设置为SUNDAY ,

// get Current Week of the year
    calendar=Calendar.getInstance();
    Log.v("Current Week", String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR)));
    int current_week=calendar.get(Calendar.WEEK_OF_YEAR);
    int week_start_day=calendar.getFirstDayOfWeek(); // this will get the starting day os week in integer format i-e 1 if monday
    Toast.makeText(getContext(),"Current Week is"+current_week +"Start Day is"+week_start_day,Toast.LENGTH_SHORT).show();


    // get the starting and ending date
    // Set the calendar to sunday of the current week
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    System.out.println("Current week = " + Calendar.DAY_OF_WEEK);

    // Print dates of the current week starting on Sunday
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    String startDate = "", endDate = "";

    startDate = df.format(calendar.getTime());
    calendar.add(Calendar.DATE, 6);
    endDate = df.format(calendar.getTime());

    System.out.println("Start Date = " + startDate);
    System.out.println("End Date = " + endDate);
Run Code Online (Sandbox Code Playgroud)