週の最初の月曜日を取得する方法??? 月曜日である2013年7月1日から始まる学期の昼食を表示する学校アプリを作成しているため、現在の月の最初の週に別のメニューが表示され、月の第2週に別のメニューが表示されるため、月の最初の週に月曜日1、火曜日1、水曜日1、木曜日1が表示されます,friday1, 月の第 2 週 monday2,tuesday2, wednesday2,thursday2,friday2, 以下は私のカレンダーです。このコードを変更して、月の第 1 週であることを検出する方法を示します。これは第 3 週の第 2 週です。 m1.t1,w1,t,1 に変更されます 実際には昼食は2週間提供され、再び復習するので、現在の週が月の第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] = "";
}
}