Android——待办事项(ToDoList)

一:实现功能
1:创建待办事项的显示与完成待办事项时的显示
2:搜索已经存在的代办事项
3:日历(阳历阴历转换)
代码如下:
MainActivity.java如下:
public class MainActivity extends AppCompatActivity {
private Button EXEInout, EXESearch;
private Button btn_List,btn_Calendar,btn_Complete;
private static EditText InputContent, SearchContent,SerialNumber,SearchSerialNumber;
private static EditText GetContent,GetSerialNumber,GetContent1,GetSerialNumber1,GetContent2,GetSerialNumber2;
private CheckBox checkBox,checkBox1,checkBox2;
public static int count = 1;
public static String ID = null;
public static String Content = null;
Dao dao = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EXEInout = (Button) findViewById(R.id.exeInput);
    EXESearch = (Button) findViewById(R.id.exeSearch);
    btn_Complete = (Button) findViewById(R.id.Complete);
    btn_List = (Button) findViewById(R.id.List);
    btn_Calendar = (Button) findViewById(R.id.Calendar);
    GetContent = (EditText) findViewById(R.id.GetContent);
    GetSerialNumber = (EditText) findViewById(R.id.GetSerialNumber);//序号
    GetContent1 = (EditText) findViewById(R.id.GetContent1);
    GetSerialNumber1 = (EditText) findViewById(R.id.GetSerialNumber1);//序号
    GetContent2 = (EditText) findViewById(R.id.GetContent2);
    GetSerialNumber2 = (EditText) findViewById(R.id.GetSerialNumber2);//序号
    SerialNumber = (EditText) findViewById(R.id.SerialNumber);
    checkBox = (CheckBox) findViewById(R.id.checkbox);
    checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
    checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
    InputContent = (EditText) findViewById(R.id.inputContent);
    SearchContent = (EditText) findViewById(R.id.SearchContent);
    SearchSerialNumber = (EditText) findViewById(R.id.SearchSerialNumber);
    //用于查找待办事项
    EXESearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dao = new Dao(MainActivity.this);
            String SearchID = SearchSerialNumber.getText().toString();
            String Content = dao.Query(SearchID);
            SearchContent.setText(Content);
        }
    });
    //用于添加待办事项
    EXEInout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                dao = new Dao(MainActivity.this);
                ID = SerialNumber.getText().toString();
                Content = InputContent.getText().toString();
                if (ID.equals("") && Content.equals(""))
                {
                    Toast.makeText(MainActivity.this,"输入的待办事项不能为空",Toast.LENGTH_SHORT).show();
                    return ;
                }
                dao.Insert(new DataBase(ID,Content));
                if (count == 1) {
                    GetSerialNumber.setVisibility(View.VISIBLE);
                    GetContent.setVisibility(View.VISIBLE);
                    checkBox.setVisibility(View.VISIBLE);//可见
                    GetSerialNumber.setText(ID);
                    GetContent.setText(Content);
                }
                if (count == 2)
                {
                    GetSerialNumber1.setVisibility(View.VISIBLE);
                    GetContent1.setVisibility(View.VISIBLE);
                    checkBox1.setVisibility(View.VISIBLE);//可见
                    GetSerialNumber1.setText(ID);
                    GetContent1.setText(Content);

                }
            if (count == 3)
            {
                GetSerialNumber2.setVisibility(View.VISIBLE);
                GetContent2.setVisibility(View.VISIBLE);
                checkBox2.setVisibility(View.VISIBLE);//可见
                GetSerialNumber2.setText(ID);
                GetContent2.setText(Content);

            }
            count++;
            if (count > 3)
                Toast.makeText(MainActivity.this,"待办事项存在太多了,请做完一些在添加吧!",Toast.LENGTH_SHORT).show();
            Log.d("Count",count+"");
            Log.d(Dao.TAG,ID);
            Log.d(Dao.TAG,Content);
            //清除数据库专用
            //for (int i = 0; i< 50;i++)
                //dao.Delete(i+"");
           // setListen();
        }
    });
    //(可见不可见占位置,只是消失,隐藏不会占据屏幕位置)
    //如果被选择就使checkbox,序号,EditText三个为隐藏,并传到另一个一个页面
 //用于提交待办事项(表示已完成)
 btn_Complete.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Intent intent = null;
         switch (JudgeCheckBoxStatus()) {
             case 1:
                 GetContent.setVisibility(View.GONE);
                 GetSerialNumber.setVisibility(View.GONE);
                 checkBox.setVisibility(View.GONE);
                 count--;//完成一件待办事项,减少一次计数,方便下一次添加待办事项
                 intent = new Intent(MainActivity.this, Completed.class);
                 break;
             case 2:
                 GetContent1.setVisibility(View.GONE);
                 GetSerialNumber1.setVisibility(View.GONE);
                 checkBox1.setVisibility(View.GONE);
                 count--;//完成一件待办事项,减少一次计数,方便下一次添加待办事项
                 intent = new Intent(MainActivity.this, Completed.class);
                 break;
             case 3:
                 GetContent2.setVisibility(View.GONE);
                 GetSerialNumber2.setVisibility(View.GONE);
                 checkBox2.setVisibility(View.GONE);
                 count--;//完成一件待办事项,减少一次计数,方便下一次添加待办事项
                 intent = new Intent(MainActivity.this, Completed.class);
                 break;
         }
         startActivity(intent);
     }
 });
 //跳转到日历视图
 btn_Calendar.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Intent intent = null;
         intent = new Intent(MainActivity.this, CalendarDemo.class);
                startActivity(intent);
     }
 });

}
//用于判断是否已经完成,存在漏洞
int  JudgeCheckBoxStatus(){
    if (checkBox.isChecked() == true){
        return 1;
    }
    if (checkBox1.isChecked() == true){
        return 2;
    }
    if (checkBox2.isChecked() == true){
        return 3;
    }
    return 0;
}
}
/***************************************************************************分割线*******************************************************************/

Android——待办事项(ToDoList)
activity_main.xml代码如下:









    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:textAllCaps="false"
            android:text="----------------全部----------------"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_below="@+id/SearchContent"/>
    <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="105dp"
            android:visibility="invisible"
            android:layout_below="@id/Search"

    />
    <EditText
            android:id="@+id/GetSerialNumber"
            android:layout_height="wrap_content"
            android:layout_width="40dp"
            android:text=""
            android:layout_marginLeft="40dp"
            android:layout_marginTop="85dp"
            android:textSize="25sp"
            android:visibility="invisible"
            android:layout_below="@+id/SearchContent"

    />
    <EditText
            android:id="@+id/GetContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="25sp"
            android:layout_marginTop="86dp"
            android:layout_marginLeft="80dp"
            android:visibility="invisible"
            android:layout_below="@+id/exeSearch"


    />
    <CheckBox
            android:id="@+id/checkbox1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="50dp"
            android:visibility="invisible"
            android:layout_below="@id/checkbox"

    />
    <EditText
            android:id="@+id/GetSerialNumber1"
            android:layout_height="wrap_content"
            android:layout_width="40dp"
            android:text=""
            android:layout_marginLeft="40dp"
            android:layout_marginTop="20dp"
            android:textSize="25sp"
            android:visibility="invisible"
            android:layout_below="@+id/GetSerialNumber"

    />
    <EditText
            android:id="@+id/GetContent1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="25sp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="80dp"
            android:visibility="invisible"
            android:layout_below="@+id/GetContent"
    />
    <CheckBox
            android:id="@+id/checkbox2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="50dp"
            android:visibility="invisible"
            android:layout_below="@id/checkbox1"

    />
    <EditText
            android:id="@+id/GetSerialNumber2"
            android:layout_height="wrap_content"
            android:layout_width="40dp"
            android:text=""
            android:layout_marginLeft="40dp"
            android:layout_marginTop="20dp"
            android:textSize="25sp"
            android:visibility="invisible"
            android:layout_below="@+id/GetSerialNumber1"

    />
    <EditText
            android:id="@+id/GetContent2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="25sp"
            android:layout_marginTop="22dp"
            android:layout_marginLeft="80dp"
            android:visibility="invisible"
            android:layout_below="@+id/GetContent1"
    />
    <Button
            android:id="@+id/List"
            android:layout_height="50dp"
            android:layout_width="120dp"
            android:text="List"
            android:textAllCaps="false"
            android:layout_marginTop="500dp"
            android:layout_alignParentLeft="true"
        />
    <Button
            android:id="@+id/Complete"
            android:layout_height="50dp"
            android:layout_width="120dp"
            android:layout_marginLeft="150dp"
            android:text="Complete"
            android:textAllCaps="false"
            android:layout_marginTop="90dp"
            android:layout_below="@+id/GetContent2"/>
    <Button
            android:id="@+id/Calendar"
            android:layout_height="50dp"
            android:layout_width="120dp"
            android:text="Calendar"
            android:textAllCaps="false"
            android:layout_marginTop="500dp"
            android:layout_alignParentRight="true"
            />

</RelativeLayout>
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


</RelativeLayout>
/***************************************************************************分割线*******************************************************************/ Completed.java代码如下 public class Completed extends AppCompatActivity { private Button btn_Receive; private RecyclerView recyclerView; private List dataBaseList = new ArrayList<>(); private MyRecyclerView myRecyclerView; private DataBase dataBase; private TextView Title; private int count = 1; Dao dao = null; private String GetContent = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_completed);
    Title = (TextView) findViewById(R.id.Title);
    recyclerView = (RecyclerView) findViewById(R.id.RecyclerViewItem);
    btn_Receive = (Button) findViewById(R.id.EXE_Receive);

    //设置布局管理器
    LinearLayoutManager layoutManager = new LinearLayoutManager(Completed.this);
    recyclerView.setLayoutManager(layoutManager);
    //设置适配器
    myRecyclerView = new MyRecyclerView(dataBaseList);
    recyclerView.setAdapter(myRecyclerView);

    btn_Receive.setOnClickListener(new Button.OnClickListener() {
        DataBase dataBase = new DataBase();
        @Override
        public void onClick(View v) {
            String CompletedID = MainActivity.ID;
            String CompletedContent = MainActivity.Content;
            if (! ("".equals(CompletedID)) && ( "".equals(CompletedContent)))
            {
                Toast.makeText(Completed.this,"内容不能为空",Toast.LENGTH_SHORT).show();
            }
            else {
                dataBase = new DataBase(CompletedID, CompletedContent);//获取实例
                dataBaseList.add(dataBase);//添加数据
                myRecyclerView.notifyItemChanged(dataBaseList.size() - 1);
                recyclerView.scrollToPosition(dataBaseList.size() - 1);
            }
        }
    });
}

/********分割线/
activity_completed.xml界面如图所示:
Android——待办事项(ToDoList)

activity_completed.xm代码如下:

<TextView
        android:id="@+id/Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="已完成事项"
        android:textSize="25sp"
        android:gravity="center"
        android:textColor="#090909"
        android:layout_marginBottom="20dp"/>
<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/RecyclerViewItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
<Button
 android:id="@+id/EXE_Receive"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="EXE_Receive"
 android:textAllCaps="false"
 android:layout_marginTop="20dp"/>
/***************************************************************************分割线*******************************************************************/ DataBase.java(实例化数据类) 代码如下: public class DataBase { private String id; private String Content; private static final int Check = 1;//用于判断状态待办事项是否已经完成 private static final int NotCheck = 0; public String getId() { return id; } public String getContent() { return Content; }
public DataBase() {
    super();
}
public DataBase(String id, String Content) {
    super();
    this.id = id;
    this.Content = Content;
}
}

/********分割线/
Sqlite数据库代码如下(实现CRUD)
public class Dao {
public static final String TAG = “DataBase”;
private Helper helper ;
private SQLiteDatabase DB;
public Dao(Context context)
{
helper = new Helper(context);
}
public void Insert(DataBase dataBase){
DB = helper.getReadableDatabase();
if (DB.isOpen())
{
ContentValues values = new ContentValues();
values.put(Helper.ID,dataBase.getId());
values.put(Helper.Content,dataBase.getContent());
long RowId = DB.insert(Helper.TableName,null,values);
if(RowId == -1)
Log.i(TAG, “数据插入失败!”);
else
Log.i(TAG, “数据插入成功!”+RowId);
}
}
//根据序号删除数据
//当前id为暂不为序号
public void Delete(String id){
DB = helper.getReadableDatabase();
if (DB.isOpen()){
String whereClause = “ID = ?”;
String[] whereArgs = {id + “”};
int count = DB.delete(Helper.TableName, whereClause, whereArgs);
if (count > 0)
Log.i(TAG, "删除了: " + count + “行”);
else
Log.i(“todolist”, “数据未删除!”);
DB.close(); // 数据库关闭
}
}
public void Update(String id ,String Content){
DB = helper.getWritableDatabase();
if(DB.isOpen()) { // 如果数据库打开, 执行添加的操作
ContentValues values = new ContentValues();
values.put(“ID”,id);
values.put(“Content”, Content);
int count = DB.update(Helper.TableName, values, “_ID = ?”, new String[]{id + “”});
if (count > 0)
Log.i(TAG, "修改了: " + count + “行”);
else
Log.i(“todolist”, “数据未删除!”);
DB.close(); // 数据库关闭
}
}
public String Query(String id){
DB = helper.getReadableDatabase();
//selection查询子句的条件,可以使用主键查询
String[] columns = {“ID”, “Content”}; // 需要的列
String selection = “ID = ?”;
String[] selectionArgs = {id + “”};
Cursor cursor = DB.query(Helper.TableName,columns,selection,selectionArgs,null,null,null);
if (cursor.moveToFirst())
{
if(cursor != null && cursor.moveToFirst()) {
//String _id = cursor.getString(0);
String Content = cursor.getString(1);
DB.close();
return Content;
}
cursor.close();
DB.close();
}
return null;
}
public List QueryAll() {
DB = helper.getReadableDatabase(); // 获得一个只读的数据库对象
if(DB.isOpen()) {
String[] columns = {“ID”, “Content”}; // 需要的列
String selection = null; // 选择条件, 给null查询所有
String[] selectionArgs = null; // 选择条件的参数, 会把选择条件中的? 替换成数据中的值
String groupBy = null; // 分组语句 group by name
String having = null; // 过滤语句
String orderBy = null; // 排序
Cursor cursor = DB.query(Helper.TableName, columns, selection, selectionArgs, groupBy, having, orderBy);
String id;
String Content;
if(cursor != null && cursor.getCount() > 0) {
List DataBaseList = new ArrayList();
while(cursor.moveToNext()) { // 向下移一位, 知道最后一位, 不可以往下移动了, 停止.
id = cursor.getString(0);
Content = cursor.getString(1);
DataBaseList.add(new DataBase(id, Content));
}
DB.close();
return DataBaseList;
}
DB.close();
}
return null;
}
}
/********分割线/
Helper类代码如下(数据库创建类继承SQLiteOpenHelper)
public class Helper extends SQLiteOpenHelper {
public static final String TableName = “ToDoList”;
public static final String ID = “ID”;
public static final String Content = “Content”;
private static final String DataBase = “todolist.db”;
private static final SQLiteDatabase.CursorFactory Factory = null;
public static final int version = 1;

public Helper(@Nullable Context context) {
    super(context, DataBase, Factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
    // 操作数据库
    String sql = "create table " + TableName + "("+ID+" varchar(20) primary key, "+Content+" varchar(20));";
    db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists "+TableName);
    onCreate(db);
}

}
/********分割线/
MyRecyclerView类代码如下(继承RecyclerView.Adapter)
//使用此类必须先导入闭包implementation ‘com.android.support:recyclerview-v7:27.1.1’(闭包版本很多)
public class MyRecyclerView extends RecyclerView.Adapter<MyRecyclerView.ViewHolder> {
private List DataBaseList;
public MyRecyclerView (List DataBaseList)
{
this.DataBaseList = DataBaseList;
}

@NonNull
@Override
//获取布局
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,parent,false);
    return new ViewHolder(view);
}
//给子控件赋值
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
      DataBase dataBase = DataBaseList.get(position);
      holder.checkBox.setChecked(true);//设置勾选
      holder.SearchSerialNumber.setText(dataBase.getId());//设置完成待办事项的序号
      holder.Content.setText(dataBase.getContent());//设置完成待办事项的内容
}

@Override
public int getItemCount() {
    return DataBaseList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
    CheckBox checkBox;
    EditText SearchSerialNumber,Content;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        checkBox = (CheckBox) itemView.findViewById(R.id.RecyclerViewCheckbox);
        SearchSerialNumber = (EditText) itemView.findViewById(R.id.RecyclerViewSerialNumber);
        Content = (EditText) itemView.findViewById(R.id.RecyclerViewContent);
    }
}

}
/********分割线/
Lunar类(用于日历的阳历和阴历转换)
package com.example.todolistdemo;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Lunar {
private final int year;
private final int month;
private final int day;
private boolean leap;
//阴历月份表示形式
final static String[] LunarMonthStyle = {“一”, “二”, “三”, “四”, “五”, “六”, “七”, “八”, “九”, “十”, “十一”, “十二”};
@SuppressLint(“SimpleDateFormat”)
static SimpleDateFormat chineseDateFormat = new SimpleDateFormat(“yyyy年MM月dd日”);
final static long[] lunarInfo = new long[]
{
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0};

//====== 传回农历 y年的总天数
final private static int yearDays(int y) {
    int i, sum = 348;
    for (i = 0x8000; i > 0x8; i >>= 1) {
        if ((lunarInfo[y - 1900] & i) != 0)
            sum += 1;
    }
    return (sum + leapDays(y));
}


//====== 传回农历 y年闰月的天数
final private static int leapDays(int y) {
    if (leapMonth(y) != 0) {
        if ((lunarInfo[y - 1900] & 0x10000) != 0)
            return 30;
        else
            return 29;
    } else
        return 0;
}


//====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
final private static int leapMonth(int y) {
    return (int) (lunarInfo[y - 1900] & 0xf);
}


//====== 传回农历 y年m月的总天数
final private static int monthDays(int y, int m) {
    if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
        return 29;
    else
        return 30;
}


//====== 传回农历 y年的生肖
final public String animalsYear() {
    final String[] Animals = new String[]{"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
    return Animals[(year - 4) % 12];
}


//====== 传入 月日的offset 传回干支, 0=甲子
final private static String cyclicalm(int num) {
    final String[] TianGan = new String[]{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
    final String[] JiaZi = new String[]{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
    return (TianGan[num % 10] + JiaZi[num % 12]);
}


//====== 传入 offset 传回干支, 0=甲子
final public String cyclical() {
    int num = year - 1900 + 36;
    return (cyclicalm(num));
}
public String getLunarMonthString() {
    // TODO Auto-generated method stub
    return null;
}


public Lunar(Calendar cal) {
    @SuppressWarnings("unused") int yearCyl, monCyl, dayCyl;
    int leapMonth = 0;
    Date baseDate = null;
    try {
        baseDate = chineseDateFormat.parse("1900年1月31日");
    } catch (ParseException e) {
        e.printStackTrace(); //To change body of catch statement use Options | File Templates.
    }


    //求出和1900年1月31日相差的天数
    assert baseDate != null;
    int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L);
    dayCyl = offset + 40;
    monCyl = 14;


    //用offset减去每农历年的天数
    // 计算当天是农历第几天
    //i最终结果是农历的年份
    //offset是当年的第几天
    int iYear, daysOfYear = 0;
    for (iYear = 1900; iYear < 2050 && offset > 0; iYear++) {
        daysOfYear = yearDays(iYear);
        offset -= daysOfYear;
        monCyl += 12;
    }
    if (offset < 0) {
        offset += daysOfYear;
        iYear--;
        monCyl -= 12;
    }
    //农历年份
    year = iYear;

    yearCyl = iYear - 1864;
    leapMonth = leapMonth(iYear); //闰哪个月,1-12
    leap = false;


    //用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天
    int iMonth, daysOfMonth = 0;
    for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {
        //闰月
        if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) {
            --iMonth;
            leap = true;
            daysOfMonth = leapDays(year);
        } else
            daysOfMonth = monthDays(year, iMonth);


        offset -= daysOfMonth;
        //解除闰月
        if (leap && iMonth == (leapMonth + 1)) leap = false;
        if (!leap) monCyl++;
    }
    //offset为0时,并且刚才计算的月份是闰月,要校正
    if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
        if (leap) {
            leap = false;
        } else {
            leap = true;
            --iMonth;
            --monCyl;
        }
    }
    //offset小于0时,也要校正
    if (offset < 0) {
        offset += daysOfMonth;
        --iMonth;
        --monCyl;
    }
    month = iMonth;
    day = offset + 1;
}


public static String getChinaDayString(int day) {
    String[] chineseTen = {"初", "十", "廿", "三"};
    int n = day % 10 == 0 ? 9 : day % 10 - 1;
    if (day > 30)
        return "";
    if (day == 10)
        return "初十";
    else
        return chineseTen[day / 10] + LunarMonthStyle[n];
}


public String toString() {
    return (leap ? "闰" : "") + LunarMonthStyle[month - 1] + "月" + getChinaDayString(day);
}

}
/********分割线/
日历类主要关于CalendarView再次不多介绍
图所示:![在这里插入图片描述](https://www.icode9.com/i/ll/?i=2021013116150059.png
Android——待办事项(ToDoList)
//麻烦大家多给一点建议

上一篇:React+Mobx+Koa2+LeanCloud 搭建个人版TodoList


下一篇:java代码------常用工具类(尚马day14作业)Week4(5)