如何忘记周的第一个星期一???我正在创建一个学期应用程序,显示从2013年1月1日开始的学期午餐,这是星期一,所以它的第二个月的另一个菜单,给第二个月的另一个菜单,所以第一个月的第一个星期显示星期一,星期二,星期三,星期四1 ,friday1,月份的第二个星期我会喜欢检查星期一2,星期二2,星期三,星期四,星期五,下面是我的日历我将如何修改此代码以检测它是星期一月这是第三周的secon周月再次值将改为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] = "";
}
}
答案 0 :(得分: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
(对于此示例)在当前月份中出现的次数。
答案 1 :(得分: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);