以下是修正后的代码:

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private CalendarView calendarView;
    private Button checkInButton;

    private AppDatabase appDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        calendarView = findViewById(R.id.calendarView);
        checkInButton = findViewById(R.id.checkInButton);

        appDatabase = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "check-in-db")
                .allowMainThreadQueries()
                .build();

        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                String selectedDate = formatDate(year, month, dayOfMonth);
                Toast.makeText(MainActivity.this, "选择的日期:" + selectedDate, Toast.LENGTH_SHORT).show();
            }
        });

        checkInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String currentDate = getCurrentDate();
                String currentTime = getCurrentTime();
                CheckIn checkIn = new CheckIn(currentDate, currentTime);
                appDatabase.checkInDao().insert(checkIn);
                Toast.makeText(MainActivity.this, "打卡成功:" + currentDate + " " + currentTime, Toast.LENGTH_SHORT).show();

                int checkInCount = appDatabase.checkInDao().getCheckInCount();
                Toast.makeText(MainActivity.this, "已打卡次数:" + checkInCount, Toast.LENGTH_SHORT).show();
            }
        });

        int checkInCount = appDatabase.checkInDao().getCheckInCount();
        Toast.makeText(MainActivity.this, "已打卡次数:" + checkInCount, Toast.LENGTH_SHORT).show();

        List<CheckIn> checkIns = appDatabase.checkInDao().getAllCheckIns();
        for (CheckIn checkIn : checkIns) {
            System.out.println("打卡记录:" + checkIn.getDate() + " " + checkIn.getTime());
        }
    }

    private String formatDate(int year, int month, int dayOfMonth) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(year - 1900, month, dayOfMonth);
        return sdf.format(date);
    }

    private String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return sdf.format(date);
    }

    private String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        return sdf.format(date);
    }
}

请注意,这个代码示例假设你已经创建了CheckIn实体类、CheckInDao接口和AppDatabase数据库类。确保这些类的代码也包含在你的项目中。

Android 日期格式化错误及修复:SimpleDateFormat 使用详解

原文地址: https://www.cveoy.top/t/topic/blUx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录