Java 如何在android中使用对象模型数据库获取日期和月份列

Java 如何在android中使用对象模型数据库获取日期和月份列,java,android,sqlite,datetime,Java,Android,Sqlite,Datetime,我在数据库sqlite中添加了静态日期,我使用对象模型得到了这个日期和月份列,并设置了日期和月份通知,假设今天是4个人生日,我收到了通知,这4个人的名字,但有错误,我通过对象得到了日期和月份列,所以得到了错误 我的数据库列 DataType TEXT Integer Integer Name dd mm "Indira Gandhi" "19" 11 "Bal Gang

我在数据库sqlite中添加了静态日期,我使用对象模型得到了这个日期和月份列,并设置了日期和月份通知,假设今天是4个人生日,我收到了通知,这4个人的名字,但有错误,我通过对象得到了日期和月份列,所以得到了错误

我的数据库列

DataType
TEXT                    Integer   Integer
Name                     dd         mm
"Indira Gandhi"         "19"        11
"Bal Gangadhar Tilak"   "23"        7
"Mangal Pandey"         "19"        7
Notification.java

People people = new People();
    Calendar calendar = Calendar.getInstance();
    try {
        String str_date = people.getDate();
        Log.d(getClass().getName(), "strDate" + str_date);
        String str_month = people.getMonth();
        DateFormat formatter;
        Date date, month;
        formatter = new SimpleDateFormat("dd-MMM");
        date = (Date) formatter.parse(str_date);
        Log.d(getClass().getName(), "str_date" + date);
        month = (Date) formatter.parse(str_month);
        Log.d(getClass().getName(), "str_month" + month);
        calendar.setTime(date);
        calendar.setTime(month);
        calendar.set(Calendar.HOUR_OF_DAY, 10);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 0);
        calendar.setTime(month);
    } catch (ParseException e) {
        e.printStackTrace();
    }


    Intent intent1 = new Intent(Notification.this, MyReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getService(Notification.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) Notification.this.getSystemService(Notification.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
MyReceiver

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub


        long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, NotificationOpen.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.birthday_icon)
            .setContentTitle("Alert Me!")
            .setContentText("Today is Birthday").setSound(alarmSound)
            .setAutoCancel(true).setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    notificationManager.notify(0, mNotifyBuilder.build());


}
 public List<People> getPeopleDate() {
    List<People> peoples = new ArrayList<>();

    try {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
        Cursor cursor = db.rawQuery("select * from people", null);
        while (cursor.moveToNext()) {
            String peopleImage = cursor.getString(cursor.getColumnIndex(PEOPLE_IMAGE));
            String peopleName = cursor.getString(cursor.getColumnIndex(PEOPLE_NAME));

            String peopleDate = cursor.getString(cursor.getColumnIndex(PEOPLE_DATE));
            String peopleMonth = cursor.getString(cursor.getColumnIndex(PEOPLE_MONTH));
            String peopleYear = cursor.getString(cursor.getColumnIndex(PEOPLE_YEAR));

            People people = new People();

            people.setPeopleImage(peopleImage);
            people.setPeopleName(peopleName);

            people.setDate(peopleDate);
            people.setMonth(peopleMonth);
            people.setYear(peopleYear);

            peoples.add(people);
        }
    } catch (Exception e) {
        Log.d("DB", e.getMessage());
    }
    return peoples;
}
数据库助手

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub


        long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, NotificationOpen.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.birthday_icon)
            .setContentTitle("Alert Me!")
            .setContentText("Today is Birthday").setSound(alarmSound)
            .setAutoCancel(true).setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
    notificationManager.notify(0, mNotifyBuilder.build());


}
 public List<People> getPeopleDate() {
    List<People> peoples = new ArrayList<>();

    try {
        SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
        Cursor cursor = db.rawQuery("select * from people", null);
        while (cursor.moveToNext()) {
            String peopleImage = cursor.getString(cursor.getColumnIndex(PEOPLE_IMAGE));
            String peopleName = cursor.getString(cursor.getColumnIndex(PEOPLE_NAME));

            String peopleDate = cursor.getString(cursor.getColumnIndex(PEOPLE_DATE));
            String peopleMonth = cursor.getString(cursor.getColumnIndex(PEOPLE_MONTH));
            String peopleYear = cursor.getString(cursor.getColumnIndex(PEOPLE_YEAR));

            People people = new People();

            people.setPeopleImage(peopleImage);
            people.setPeopleName(peopleName);

            people.setDate(peopleDate);
            people.setMonth(peopleMonth);
            people.setYear(peopleYear);

            peoples.add(people);
        }
    } catch (Exception e) {
        Log.d("DB", e.getMessage());
    }
    return peoples;
}
}


您将上述值作为字符串(cursor.getString())获取,这些值是定义为字符串还是int?您可以发布创建表查询。

共享代码中有很多混乱,如:

String peopleImage = cursor.getString(cursor.getColumnIndex(PEOPLE_IMAGE));
            String peopleName = cursor.getString(cursor.getColumnIndex(PEOPLE_NAME));

            String peopleDate = cursor.getString(cursor.getColumnIndex(PEOPLE_DATE));
            String peopleMonth = cursor.getString(cursor.getColumnIndex(PEOPLE_MONTH));
            String peopleYear = cursor.getString(cursor.getColumnIndex(PEOPLE_YEAR));

            People people = new People();

            people.setPeopleImage(peopleImage);
            people.setPeopleName(peopleName);

            people.setDate(peopleDate);
            people.setMonth(peopleMonth);
            people.setYear(peopleYear);

            peoples.add(people);
**您设置了很多值,但您的人员类只有:**

public class People implements Serializable {

 private String month, date;

  public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getMonth() {
    return month;
}

public void setMonth(String month) {
    this.month = month;
}
因此,我建议您共享人物类完整信息和表格描述。

将月份名称转换为整数 您的问题和代码非常复杂,但您似乎对将月份以英文名称存储为文本挂起了钩

首先,不要这样做,不要储存一个月的名字。使用1-12的纯整数

但如果必须,可以将英文名称month转换为
month
enum对象,因为enum对象恰好是用英文标签定义的。从该
Month
对象请求整数月数

Month m = Month.valueOf( "NOVEMBER" );
int monthNumber = m.getValue();
此数字加上月日数字可用于获取
MonthDay
对象,并以下面代码中所示的方式使用

MonthDay md = MonthDay.of( monthNumber , dayOfMonthNumber );
ISO 8601 使用标准ISO 8601格式将定期周年纪念存储为文本数据类型中的组合月日值:
--MM-DD
。这是使用连字符代替年份的标准日期格式,
YYYY-MM-DD
。这种格式的好处包括使其与其他日期时间格式明确无误,并且按字母顺序排序也是按时间顺序的

MonthDay
java.time类
MonthDay
表示不带年份和时区的本月日值。此类还知道如何以上述标准格式生成和解析字符串

String mdString = myResultSet.getString( … );
MonthDay md = MonthDay.parse( mdString );
ZoneDateTime
现在知道今天的日期。这样做需要一个时区,因为对于任何给定的时刻,全球各地的日期都会因区域而异

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( z );
LocalDate
从中我们可以提取一个仅日期值

LocalDate today = now.toLocalDate();
获取当前年份,以便确定生日

int year = today.atYear();
LocalDate birthday= md.atYear( year );
那个生日可能已经过去了,我们想要的是未来

if( ! birthday.isAfter( today ) ) {
    birthday = birthday.plusYears( 1 );
}
持续时间
获取从现在到未来生日之间的时间跨度,创建
持续时间。显然你的报警系统需要毫秒。所以我们需要一天中的时间。我们将从今天的第一刻开始。但是你可能想要一天中比午夜晚一点的时间来触发警报

ZonedDateTime alarm = birthday.atStartOfDay();
Duration d = Duration.between( now , alarm );
long millis = d.toMillis();
安卓 在ThreeTen Backport项目中,java.time的大部分功能都被向后移植到Java6和Java7。在ThreeTenABP项目中进一步适应Android

提示 不要把一个月的某一天称为“日期”,因为它是不明确的。遵循java.time类中的命名


不要将月份存储为英文名称。为1月至12月存储整数1-12更简单。在Java代码中,使用
Month
enum对象,而不是文本或数字。

我认为您正在尝试转换“十一月”到int。这是不可能的。在静态数据库检查更新Qu中,11月已到int@DheerubhaiBansal@JaydeepDhamecha请共享人物类和人物表的完整描述好的,我将共享如何在mm中以整数类型存储字符串值。我没有创建表,我从数据库直接@Ankit Dubey获取值,但月份是显示通知类检查上面的我的图像,以及如何在my code@Basil BourqueI中实现此代码。我无法理解您的评论。