Android 日期格式化与 Room 数据库:构建打卡应用

这篇文章将指导你创建一个简单的 Android 打卡应用,学习如何使用 SimpleDateFormat 类格式化日期,并使用 Room 持久化库保存和检索打卡记录。

问题

你可能会遇到需要在 Android 应用中处理日期和时间的情况,例如创建一个打卡应用来记录用户打卡的时间。为了实现这个功能,你需要使用 SimpleDateFormat 类将日期格式化为 'yyyy-MM-dd' 的形式。

解决方案

以下代码演示了如何使用 SimpleDateFormat 格式化日期,以及如何使用 Room 数据库保存和检索打卡记录:javaimport 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);
Android 日期格式化与 Room 数据库:构建打卡应用

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

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