根据日期计算星期的公式有很多,下面介绍一个比较著名的——蔡勒(Zeller)公式,即w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1。
公式中的符号含义如下:C是世纪数减一,y是年份后两位,M是月份,d是日数。1月和2月要按上一年的13月和14月来算,这时C和y均按上一年取值。
算出来的W除以7,余数是几就是星期几。如果余数是0,则为星期日。
这里需要注意:我在测试的时候发现有些日期算出来的w除以7的结果是负数(-1~-6),这时需要加7.
下面通过一个实例来演示:
Activity:
package com.home.week; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private Button setBtn; private TextView showDateText; private TextView showWeekText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setBtn = (Button) findViewById(R.id.activity_main_btn); setBtn.setOnClickListener(this); showDateText = (TextView) findViewById(R.id.activity_main_tv_show_date); showWeekText = (TextView) findViewById(R.id.activity_main_tv_show_week); } @Override public void onClick(View v) { getDateAndWeek(); } /** * 弹出日期对话框并获取选择的日期和星期 */ private void getDateAndWeek() { Calendar c = Calendar.getInstance(); int year = 0; int mouth = 0; int day = 0; if (!"".equals(showDateText.getText().toString())) { String date = showDateText.getText().toString(); String[] strs = date.split("-"); year = Integer.parseInt(strs[0]); mouth = Integer.parseInt(strs[1]) - 1; day = Integer.parseInt(strs[2]); } else { year = c.get(Calendar.YEAR); mouth = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); } // 直接创建一个DatePickerDialog对话框实例,并将它显示出来 new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { showDateText.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth); int c = year / 100; int d = dayOfMonth; int y = year % 100; int m = monthOfYear + 1; if (m == 1 || m == 2) { y = year - 1; m = monthOfYear + 12; } // 运用Zeller公式计算星期 int w = (y + (y / 4) + (c / 4) - 2 * c + (26 * (m + 1) / 10) + d - 1) % 7; if (w < 0) { w += 7; } if (monthOfYear == 0 || monthOfYear == 1) { w += 2; } if (w >= 7) { w = w % 7; } String week = null; switch (w) { case 0: week = "日"; break; case 1: week = "一"; break; case 2: week = "二"; break; case 3: week = "三"; break; case 4: week = "四"; break; case 5: week = "五"; break; case 6: week = "六"; break; default: break; } showWeekText.setText("星期" + week); } } // 设置初始日期 , year, mouth, day).show(); } }
布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/activity_main_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="设置日期" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" > <TextView android:id="@+id/activity_main_tv_show_date_prompt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="日期:" /> <TextView android:id="@+id/activity_main_tv_show_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_toRightOf="@id/activity_main_tv_show_date_prompt" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" > <TextView android:id="@+id/activity_main_tv_show_week_prompt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="星期:" /> <TextView android:id="@+id/activity_main_tv_show_week" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_toRightOf="@id/activity_main_tv_show_week_prompt" /> </RelativeLayout> </LinearLayout>