如何显示周日至周六的当前周日期

时间:2013-09-13 18:29:20

标签: android

如何显示当前周日期从星期日开始到星期日?我创建的应用程序显示当前周日期点击下周或上周显示下一个或上一个日期我的代码在所有7个按钮上显示相同的日期

    public class MainActivity extends Activity {

public static int mYear;

public static int mMonth;
public static int mDay;

Button last_week;
Button next_week;
Button e00;
Button e01;
Button e02;
Button e03;
Button e04;
Button e05;
Button e06;

String completedate;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    e00 = (Button) findViewById(R.id.e00);
    e01 = (Button) findViewById(R.id.e01);
    e02 = (Button) findViewById(R.id.e02);
    e03 = (Button) findViewById(R.id.e03);
    e04 = (Button) findViewById(R.id.e04);
    e05 = (Button) findViewById(R.id.e05);
    e06 = (Button) findViewById(R.id.e06);

//  e01 = Mon;
//  e02 = Tues;
//  e03 = Wed;
//  e04 = thur;
//  e05 = Fri;


    last_week = (Button) findViewById(R.id.last_week);
    next_week = (Button) findViewById(R.id.next_week);


    Calendar mCalendar = Calendar.getInstance();
    mYear = mCalendar.get(Calendar.YEAR);
    mMonth = mCalendar.get(Calendar.MONTH) + 1;
    mDay = mCalendar.get(Calendar.DAY_OF_MONTH);

    completedate = mYear + "/" + String.format("%02d", mMonth)
            + "/" + String.format("%02d", mDay);

    last_week.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            e00.setText("");
            e01.setText("");
            e02.setText("");
            e03.setText("");
            e04.setText("");
            e05.setText("");
            e06.setText("");

            }

    });
    next_week.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            e00.setText("");
            e01.setText("");
            e02.setText("");
            e03.setText("");
            e04.setText("");
            e05.setText("");
            e06.setText("");
            }


    });

   // i want to show like this

   //       e01.setText(" 9/9/2013");
  //        e02.setText("10/9/2013");
  //        e03.setText("11/9/2013");
   //       e04.setText("12/9/2013");
   //       e05.setText("13/9/2013");




    DateFormat format = new SimpleDateFormat(completedate);
    Calendar calendar = Calendar.getInstance();
    calendar.setFirstDayOfWeek(Calendar.SUNDAY);
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

    String[] days = new String[7];
    for (int i = 0; i < 7; i++)
    {
        days[i] = format.format(calendar.getTime());

        e00.setText(days[0]);
        e01.setText(days[1]);
        e02.setText(days[2]);
        e03.setText(days[3]);
        e04.setText(days[4]);
        e05.setText(days[5]);
        e06.setText(days[6]);



    }


}

1 个答案:

答案 0 :(得分:0)

calendar.getTime()返回当前时间。我认为你会想要使用:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);

在这种情况下,-1会在一天前给你,+1会在将来给你一天。因此,有了这些知识,您就可以跟踪从今天到显示周的时间差异。

这有意义吗?

编辑:

//positive numbers indicate future, negative past
int numberOfWeeksSinceTodaysDate = 0;
int dayOfTheWeek = cal.get(Calendar.DAY_OF_WEEK)

last_week.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Calendar cal = Calendar.getInstance();

        //Go back seven days
        numberOfWeeksSinceTodaysDate -= 7;
        cal.add(Calendar.DATE, numberOfWeeksSinceTodaysDate)
        e00.setText(cal.getTime());     

        //Go back eight days
        numberOfWeeksSinceTodaysDate--;
        cal.add(Calendar.DATE, numberOfWeeksSinceTodaysDate)
        e01.setText(cal.getTime());
        }

});