如何在android中获取下一周和上一周的日期

时间:2017-07-19 08:11:07

标签: android

wp-admin/options-general.php

这是我的代码,我将以2017-07-17至2017-07-23的特定时间格式获取当前的工作日期。

public  String getCurrentWeekDate() {
    Calendar c = GregorianCalendar.getInstance();
    System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    String startDate = "", endDate = "";
    startDate = df.format(c.getTime());
    c.add(Calendar.DATE, 6);
    endDate = df.format(c.getTime());
    System.out.println("Start Date = " + startDate);
    System.out.println("End Date = " + endDate);

    return  "";
}

这是我的代码,我必须在下周和上周的日期显示,请建议我如何增加和减少星期日我是android新手。

4 个答案:

答案 0 :(得分:1)

下周calendar.add(Calendar.DAY_OF_WEEK, 1);

前一周calendar.add(Calendar.DAY_OF_WEEK, -1);

public void getCurrentWeekDate(int week) {
    Calendar c = GregorianCalendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    c.add(Calendar.DAY_OF_WEEK, week);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    String startDate;
    String endDate;
    startDate = df.format(c.getTime());
    c.add(Calendar.DAY_OF_MONTH, 6);
    endDate = df.format(c.getTime());
    System.out.println("Start Date = " + startDate);
    System.out.println("End Date = " + endDate);
}

答案 1 :(得分:0)

由于您已经找到当前的一周的日期,所以您要做的就是减去前一周日期的7天,并为下周的日期添加7天。

您可以通过以下代码

来实现
 public void getCurrentWeekDate(int week) {
    Calendar c = GregorianCalendar.getInstance();
    System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
    c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    System.out.println("Current week = " + Calendar.DAY_OF_WEEK);
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    String startDate;
    String endDate;
    startDate = df.format(c.getTime());

    // previous week calculation
    Calendar previousWeekCalendar = c;
    previousWeekCalendar.add( Calendar.DAY_OF_YEAR, -7);
    startPreviousWeekDate = df.format(previousWeekCalendar.getTime());
    previousWeekCalendar.add(Calendar.DATE, 6);
    endPreviousWeekDate = df.format(previousWeekCalendar.getTime());

    c.add(Calendar.DATE, 6);
    //for previous week
    //c.add(Calendar.DAY_OF_WEEK, -1);
    //for next week
    c.add(Calendar.DAY_OF_WEEK, week);
    endDate = df.format(c.getTime());


    // Do next week calculation same as previous week. Just check what is the value of c before starting the calculation
    System.out.println("Start Date = " + startDate);
    System.out.println("End Date = " + endDate);
    System.out.println("End Date = " + endDate);

}

所以你的代码如下所示 -

undefined method 'video' for #<Video:0xbe430a0>

答案 2 :(得分:0)

很多次尝试我都得到了这个结果:

fun getWeekDates(currentWeek: Int): Array<String?> {
   val daysOfWeek = arrayOfNulls<String>(7)
   val calendar = Calendar.getInstance()
   calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
   calendar.set(Calendar.WEEK_OF_MONTH, currentWeek)
   for (i in 0..6) {
      daysOfWeek[i] = changeDateFormat(LocalDate(calendar.time))
      calendar.add(Calendar.DAY_OF_MONTH, 1)
  }
  return daysOfWeek
}

答案 3 :(得分:0)

如果您想要上周的开始结束日期,请将 currentWeekNumber 减 1

否则如果您希望下周开始结束日期将currentWeekNumber增加1

    //initialize Calendar
    Calendar c = Calendar.getInstance();
    
    //get current week number
    int currentWeekNumber = c.get(Calendar.WEEK_OF_YEAR);
    
    //decrement week number by 1 for the previous week
    c.set(Calendar.WEEK_OF_YEAR, currentWeekNumber - 1);

    //increment week number by 1 for the next week
    //c.set(Calendar.WEEK_OF_YEAR, currentWeekNumber + 1);        

    //set first day of week
    c.set(Calendar.DAY_OF_WEEK, 1);
    
    //get start date of previous week
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    String startDate = df.format(c.getTime());
    
    //set last day of week
    c.set(Calendar.DAY_OF_WEEK, 7);
    
    //get end date of previous week
    String endDate = df.format(c.getTime());

    Log.d("LOG_TAG", "Start Date = " + startDate);
    Log.d("LOG_TAG", "End Date = " + endDate);