0

如何度过一周的第一个星期一???我正在创建一个学校应用程序,它显示从 2013 年 7 月 1 日星期一开始的学期午餐,因此它在每月的第一周提供另一个菜单,并在每月的第二周提供另一个菜单,因此每月的第一周显示 Monday1、tuesday1、wednesday1、thursday1 ,friday1,每月的第二周,我想检查 monday2,tuesday2,wednesday2,thursday2,friday2,下面是我的日历,我将如何修改此代码以检测它是每月的第一周,这是第三周的第二周再次值将更改为 m1.t1,w1,t,1 实际上提供 2 周的午餐并再次重新安排,所以我将如何以编程方式检测当前周是每月的第一周,当前周是每月的第二周

         public class HoyahCalendar extends Activity {
public static int mYear;
public static int currentIndex = -1;
public static int mMonth;
public static int mDay;
public static String[][] a = new String[6][7];
String  January="January";
String  February="February";
String  March="March";
String  April="April";
String  May="May";
String  June="June";
String  Jully="Jully";
String  August="August";
String  September="September";
String  October="October";
String  November="November";
String  December="December";
String  Monthname;

 TextView date_today;
 ImageView last_month;
 ImageView next_month;

 ImageView last_week;
 ImageView next_week;
 String completedate2;
 Date dt1;


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

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getIntent().setAction("Already created");


    date_today = (TextView) findViewById(R.id.date_today);
    last_month = (ImageView) findViewById(R.id.last_month);
    next_month = (ImageView) findViewById(R.id.next_month);
    last_week = (ImageView) findViewById(R.id.last_week);
    next_week = (ImageView) findViewById(R.id.next_week);
  //
   //       e00 = (TextView) findViewById(R.id.e00);
   //       e01 = (TextView) findViewById(R.id.e01);
    //      e02 = (TextView) findViewById(R.id.e02);
     //     e03 = (TextView) findViewById(R.id.e03);
     //     e04 = (TextView) findViewById(R.id.e04);
     //     e05 = (TextView) findViewById(R.id.e05);
   //       e06 = (TextView) findViewById(R.id.e06);




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

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

    // / setListeners();

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

            if (mMonth == 1) {
                mYear -= 1;
                mMonth = 12;
                new ShowCalendar(mYear, mMonth, 1);
                showOnScreen();
            } else {
                mMonth -= 1;
                new ShowCalendar(mYear, mMonth, 1);
                showOnScreen();
            }

        }
    });

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

            if (mMonth == 12) {
                mYear += 1;
                mMonth = 1;
                new ShowCalendar(mYear, mMonth, 1);
                showOnScreen();
            } else {
                mMonth += 1;
                new ShowCalendar(mYear, mMonth, 1);
                showOnScreen();
            }

        }
    });

    last_week.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (mMonth == 1) {
                mYear -= 1;
                mMonth = 12;
                new ShowCalendar(mYear, mMonth, mDay, "last");
                showOnScreen();
            } else {
                // mMonth -= 1;
                new ShowCalendar(mYear, mMonth, mDay, "last");
                showOnScreen();
            }
        }
    });

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

            if (mMonth == 12) {
                mYear += 1;
                mMonth = 1;
                new ShowCalendar(mYear, mMonth, mDay, "next");
                showOnScreen();
            } else {
                if (HoyahCalendar.currentIndex == 4) {
                    HoyahCalendar.currentIndex = 4;
                    // mMonth += 1;
                }

                new ShowCalendar(mYear, mMonth, mDay, "next");
                showOnScreen();
            }

        }
    });

    new ShowCalendar(mYear, mMonth);
    showOnScreen();
     completedate2= String.format("%02d", mDay)+ "/"+String.format("%02d", 
          mMonth) +"/"+mYear;


     String input_date=completedate2;
      SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy");

    try {
        dt1 = format1.parse(input_date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      SimpleDateFormat format2=new SimpleDateFormat("EEEE"); 
      String finalDay=format2.format(dt1);
         Toast.makeText(this, "Today is is"+ finalDay, 
          Toast.LENGTH_SHORT).show();






}

public void showOnScreen() {

    if (mMonth ==1) 
    {
        Monthname="January";
        }
    else 
        if (mMonth ==2) { 
            Monthname="February";
            }

        else
    if (mMonth ==3) { Monthname="March";}

   else 
        if (mMonth ==4) { Monthname="April";    }

        else
    if (mMonth ==5) { Monthname="May";}
    else 
        if (mMonth ==6) { Monthname="June"; }
        else
    if (mMonth ==7) { Monthname="July";}
    else 
        if (mMonth ==8) { Monthname="August";   }
        else
    if (mMonth ==9) { Monthname="September";}
    else 
        if (mMonth ==10)    { Monthname="October";  }
    if (mMonth ==11)    { Monthname="November";}
    else 
        if (mMonth ==12)    { Monthname="December"; }


    date_today.setText(  Monthname + "  " +mYear);
    e00.setText("" + a[0][0]);

    if(e00.getText().toString().equals(String.valueOf(mDay)))

//  if(e00.getText().toString().equals(mDay))

    {e00.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button1 text equals!", Toast.LENGTH_SHORT).show();

    }
    e01.setText("" + a[0][1]);

    if(e01.getText().toString().equals(String.valueOf(mDay)))


    {
        e01.setTextColor(Color.parseColor("#FFBBFF"));

    Toast.makeText(this, "Button2 text equals!", Toast.LENGTH_SHORT).show();

    }

    e02.setText("" + a[0][2]);
    if(e02.getText().toString().equals(String.valueOf(mDay)))

    {e02.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button3 text equals!", Toast.LENGTH_SHORT).show();

    }
    e03.setText("" + a[0][3]);
    if(e03.getText().toString().equals(String.valueOf(mDay)))
    {e03.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button4 text equals!", Toast.LENGTH_SHORT).show();

    }
    e04.setText("" + a[0][4]);
    if(e04.getText().toString().equals(String.valueOf(mDay)))
    {e04.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button5 text equals!", Toast.LENGTH_SHORT).show();

    }
    e05.setText("" + a[0][5]);
    if(e05.getText().toString().equals(String.valueOf(mDay)))
    {e05.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button6 text equals!", Toast.LENGTH_SHORT).show();

    }
    e06.setText("" + a[0][6]);
    if(e06.getText().toString().equals(String.valueOf(mDay)))
    {e06.setTextColor(Color.parseColor("#FFBBFF"));
    Toast.makeText(this, "Button7 text equals!", Toast.LENGTH_SHORT).show();

    }

}









  public class ShowCalendar {
  int mYear;
   int mMonth;
    int mDay;
public ShowCalendar(int mYear, int mMonth){
    this.mYear = mYear;
    this.mMonth = mMonth;
    calculateMonthFirstday();
}
public ShowCalendar(int mYear, int mMonth, int mDay){
    this.mYear = mYear;
    this.mMonth = mMonth;
    HoyahCalendar.currentIndex = 0;

    this.mDay = mDay;
    calculateMonthFirstday();
}


public int getmDay() {
    return mDay;
}





public void setmDay(int mDay) {
    this.mDay = mDay;
}





public ShowCalendar(int mYear, int mMonth, int mDay, String time){
    this.mYear = mYear;
    this.mMonth = mMonth;
    if (time == "next"){
        HoyahCalendar.currentIndex++;
        if (HoyahCalendar.currentIndex == 5){
            HoyahCalendar.currentIndex--;
        }
        this.mDay = mDay + 7;
    } else if (time == "last"){
        HoyahCalendar.currentIndex--;
        if (HoyahCalendar.currentIndex == -1){
            HoyahCalendar.currentIndex++;
        }
        this.mDay = mDay - 7;
    }
    calculateMonthFirstday();
}

public void calculateMonthFirstday(){
    int month, first_day=0;
    if((mYear%4==0 && mYear%100!=0)||(mYear%400==0))
        month=1;
    else
        month=0;

    int y, y12, c, c12, m, d;
    y = mYear%100;
    y12 = (mYear-1)%100; //only for January and February
    c = mYear/100;
    c12 = (mYear-1)/100;
    m = mMonth;
    d = 1;

    switch(mMonth){
    case 1: {first_day = y12 + y12/4 +c12/4 - 2*c12 + 26*(13 + 1)/10 + d - 1;break;}
    case 2: {first_day = y12 + y12/4 +c12/4 - 2*c12 + 26*(14 + 1)/10 + d - 1;break;}
    case 3: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
    case 4: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
    case 5: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
    case 6: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
    case 7: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
    case 8: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
    case 9: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
    case 10: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
    case 11: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
    case 12: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
    }
    if(first_day<0)
        first_day = 7 - (Math.abs(first_day))%7;//first_day每月第一天星期几
    else
        first_day = first_day%7;

    switch(mMonth){
        case 1: {CalculateCalendar(1,first_day,31);break;}
        case 2: {CalculateCalendar(2,first_day,28+month);break;}
        case 3: {CalculateCalendar(3,first_day,31);break;}
        case 4: {CalculateCalendar(4,first_day,30);break;}
        case 5: {CalculateCalendar(5,first_day,31);break;}
        case 6: {CalculateCalendar(6,first_day,30);break;}
        case 7: {CalculateCalendar(7,first_day,31);break;}
        case 8: {CalculateCalendar(8,first_day,31);break;}
        case 9: {CalculateCalendar(9,first_day,30);break;}
        case 10:{CalculateCalendar(10,first_day,31);break;}
        case 11:{CalculateCalendar(11,first_day,30);break;}
        case 12:{CalculateCalendar(12,first_day,31);break;}
    }
}

public void CalculateCalendar(int month_no, int week_no, int month_days){

    int i, s, targetRow = 0;
    int currentDay;
    if (this.mDay == 0){
        mDay = 1;
        currentDay= HoyahCalendar.mDay;     
    }else {
        currentDay = this.mDay;
    }
    //String[][] a = new String[6][7];
    for (i=0;i<week_no;i++)
        HoyahCalendar.a[i/7][i%7] = "";

    for(i=week_no; i<week_no + month_days; i++){
        s = i - week_no + 1;
        HoyahCalendar.a[i/7][i%7] = String.valueOf(s);
        if (s == currentDay && HoyahCalendar.currentIndex == -1){
            HoyahCalendar.currentIndex = i/7;
        }
    } 
    for (i=0; i<7;i++){
        if (HoyahCalendar.a[HoyahCalendar.currentIndex][i] == null){
            HoyahCalendar.a[0][i] = "";
        }else{
            HoyahCalendar.a[0][i] = 
     HoyahCalendar.a[HoyahCalendar.currentIndex][i];
        }

    }
    for(i=week_no+month_days; i<42; i++)


        HoyahCalendar.a[i/7][i%7] = "";
}

    }
4

2 回答 2

1

你可能会使用Calendar.WEEK_OF_MONTH. 如果一周是第一周,那么您将获得1价值。您可以执行以下操作:

Calendar date = Calendar.getInstance();
int weekNo1 = date.get(Calendar.WEEK_OF_MONTH);

如果您看到WEEK_OF_MONTH,它会说:

This indicates the week number within the current month. The first week of the month, as defined by getFirstDayOfWeek() and getMinimalDaysInFirstWeek(), has value 1

如果您想在任何其他月份获取此信息,只需在日历中设置日期以获取该月的一周,例如:

date.set(2013, Calendar.JANUARY, 25); // Set any other months etc

您可以使用将一周的第一天设置为星期一

date.setFirstDayOfWeek(Calendar.MONDAY);

更新

Calendar date = Calendar.getInstance();
date.setFirstDayOfWeek(Calendar.MONDAY);
int weekNo1 = date.get(Calendar.WEEK_OF_MONTH);

这将告诉它将每个月的第一天设置为星期一。因此,现在对于当前日期date.get(Calendar.WEEK_OF_MONTH);将告诉您当前月份中的当前周,请记住一周的第一天是星期一。我认为您现在可以将函数可视化,以给出本月出现date.get(Calendar.WEEK_OF_MONTH)的次数(对于此示例)。Monday

于 2013-08-25T13:01:39.713 回答
1
  Calendar ca1 = Calendar.getInstance();
        ca1.set(2013, Calendar.AUGUST, 25);
        cal.setFirstDayOfWeek(Calendar.MONDAY)
        int wk = ca1.get(Calendar.WEEK_OF_MONTH);
        System.out.println("Week of Month :" + wk);
于 2013-08-25T13:07:06.520 回答