今天写了打卡关于记录和检测连续打卡时长的代码
package com.example.daka1; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.icu.util.Calendar; import android.os.Bundle; import android.view.View; import android.webkit.WebView; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.util.Date; public class Dakajiemian extends AppCompatActivity { private MySQLiteHelper mySQLiteHelper; DatePicker datePicker; int year; //今年 int month; //当前月份 int day; //今天 private EditText guanjianzi,meirizongjie; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dakajiemian); Button querendaka=(Button)findViewById(R.id.querendaka); guanjianzi=findViewById(R.id.shuru_guanjianzi); meirizongjie=findViewById(R.id.shuru_meirizongjie); Calendar calendar= Calendar.getInstance(); //获取日历的实例 year=calendar.get(Calendar.YEAR); month=calendar.get(Calendar.MONTH);//实际月份需要加1 day=calendar.get(Calendar.DATE); final int[] yyear = new int[1]; final int[] mmonth = new int[1]; final int[] dday = new int[1]; datePicker=(DatePicker)findViewById(R.id.timetable); datePicker.init(year,month,day,new DatePicker.OnDateChangedListener(){ @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { yyear[0] =year; mmonth[0] =monthOfYear; dday[0] =dayOfMonth; } }); Thesql thesql =new Thesql(this); xianshilianxu(year,month,day); Userxinxi userxinxi = (Userxinxi) getApplication(); String username=userxinxi.getLoginName(); final Userpd[] userpd = {new Userpd()}; userpd[0] =thesql.chaxun(username); TextView textView = findViewById(R.id.zuichangjianchi); textView.setText("最长打卡"+ userpd[0].zuichang+"天"); querendaka.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String guanjianzi1=guanjianzi.getText().toString(); String meirizingjie1=meirizongjie.getText().toString(); String riqi; if(yyear[0]!=0) { riqi = yyear[0] + "-" + (mmonth[0]+1) + "-" + dday[0]; }else { riqi = year + "-" + (month+1) + "-" + day; } String xuan=(yyear[0]+"")+(mmonth[0]+"")+(dday[0]+""); String jin=(year+"")+(month+"")+(day+""); if(thesql.dakachaxun(riqi)!=null) { showMsg("该日已打卡"); }else if (xuan.compareTo(jin)>0){ showMsg("时间未到"); }else { thesql.adddakajilu(riqi, guanjianzi1, meirizingjie1,username); showMsg("打卡成功"); int i=xianshilianxu(year,month,day); if(i> userpd[0].zuichang) { userpd[0].zuichang = i; thesql.xiugai(username, userpd[0]); } userpd[0] =thesql.chaxun(username); TextView textView = findViewById(R.id.zuichangjianchi); textView.setText("最长打卡"+ userpd[0].zuichang+"天"); } } }); } private int xianshilianxu(int year,int month,int day) { int jianchishijian=0; Thesql thesql =new Thesql(this); TextView textView1 = findViewById(R.id.yijianchi); String riqi1 = year + "-" + (month + 1) + "-" + day; if (thesql.dakachaxun(riqi1) != null) { jianchishijian = muqianlianxu(year, month, day,true); String yijianchitext="已坚持"+jianchishijian+"天"; textView1.setText(yijianchitext); }else { jianchishijian = muqianlianxu(year, month, day,false); String yijianchitext="已坚持"+jianchishijian+"天"; textView1.setText(yijianchitext); } return jianchishijian; } private int muqianlianxu(int year,int month,int day,boolean flag)//flag=true指今天已经打卡 { int tianshu=0; String riqi; Thesql thesql =new Thesql(this); Calendar calendar= Calendar.getInstance(); calendar.set(year,month,day); if(!flag) { calendar.add(Calendar.DAY_OF_MONTH, -1); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH);//实际月份需要加1 day = calendar.get(Calendar.DATE); } while(true) { riqi = year + "-" + (month + 1) + "-" + day; if (thesql.dakachaxun(riqi) != null) { tianshu++; calendar.add(Calendar.DAY_OF_MONTH, -1); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH);//实际月份需要加1 day = calendar.get(Calendar.DATE); } else { break; } } return tianshu; } private void showMsg(String msg){ Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } }
标签:总结,17,int,month,23.3,calendar,year,Calendar,day From: https://www.cnblogs.com/laobing6214/p/17228343.html