我在数据库 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);
我的接收者
@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;
}
People.class
public class People implements Serializable {
private String peopleImage;
private String peopleName;
private String id;
private String month, date, year;
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;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setPeopleName(String peopleName) {
this.peopleName = peopleName;
}
public String getPeopleName() {
return peopleName;
}
public void setPeopleImage(String peopleImage) {
this.peopleImage = peopleImage;
}
public String getPeopleImage() {
return peopleImage;
}
最佳答案
月份名称到整数
您的问题和代码非常复杂,但您似乎对按英文名称将月份存储为文本不感兴趣。
首先,不要这样做,不要按名称存储月份。使用普通整数,1-12 表示一月至十二月。
但如果必须,您可以将月份的英文名称转换为 Month
枚举对象,因为枚举对象恰好是用英文标签定义的。从那个 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
。这种格式的好处包括与其他日期时间格式相比没有歧义,并且按字母顺序排序也是按时间顺序排列。
月日
java.time 类 MonthDay
表示这个没有年份和时区的月-日值。此类还知道如何生成和解析上述标准格式的字符串。
String mdString = myResultSet.getString( … );
MonthDay md = MonthDay.parse( mdString );
ZonedDateTime
现在获取今天的日期。这样做需要一个时区,因为对于任何给定时刻,日期在全局范围内因时区而异。
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( z );
本地日期
从中我们可以提取一个仅限日期的值。
LocalDate today = now.toLocalDate();
获取当前年份,以便我们确定生日。
int year = today.atYear();
LocalDate birthday= md.atYear( year );
那个生日可能已经过去了,我们想要的是 future 。
if( ! birthday.isAfter( today ) ) {
birthday = birthday.plusYears( 1 );
}
持续时间
获取从现在到 future 生日之间的时间跨度,创建一个Duration
。显然你的警报系统需要几毫秒。所以我们需要一天中的时间。我们将在这里度过一天的第一刻。但是您可能希望在午夜之后的某个时间触发警报!
ZonedDateTime alarm = birthday.atStartOfDay();
Duration d = Duration.between( now , alarm );
long millis = d.toMillis();
安卓
ThreeTen-Backport 项目的许多 java.time 功能都被反向移植到 Java 6 和 7。在ThreeTenABP项目中进一步适配Android。
提示
不要将某天称为“日期”,因为它不明确。遵循在 java.time 类中看到的命名。
不要将月份存储为英文名称。为 1 月至 12 月存储整数 1-12 更简单。在您的 Java 代码中,使用 Month
枚举对象而不是文本或数字。
关于java - 如何在android中使用对象模型数据库获取日期和月份列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40123215/