我在数据库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());
}
DataBaseHelper
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;
}
}
答案 0 :(得分:2)
您的问题和代码非常令人费解,但您似乎因为英文名称而将文章存储为月份。
首先,不要这样做,不要以名字存放一个月。使用普通整数,1-12表示1月至12月。
但如果必须,您可以将月份的英文名称转换为Month
枚举对象,因为枚举对象恰好使用英文标签定义。从Month
对象请求整数月份数。
Month m = Month.valueOf( "NOVEMBER" );
int monthNumber = m.getValue();
此数字加上日期号码可用于获取MonthDay
对象,并以下面的代码中所示的方式使用。
MonthDay md = MonthDay.of( monthNumber , dayOfMonthNumber );
使用标准ISO 8601格式在文本数据类型中将周期性周年纪念存储为合并的月日值:--MM-DD
。这是使用连字符代替年份的标准日期格式YYYY-MM-DD
。这种格式的好处包括明确地使用其他日期时间格式,按字母顺序排序也是按时间顺序排列的。
MonthDay
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
由此我们可以提取仅限日期的值。
LocalDate today = now.toLocalDate();
获取当前年份,以便我们确定生日。
int year = today.atYear();
LocalDate birthday= md.atYear( year );
那个生日可能已经过去了,我们想要未来。
if( ! birthday.isAfter( today ) ) {
birthday = birthday.plusYears( 1 );
}
Duration
获取现在和未来生日之间的时间跨度,创建Duration
。显然你的报警系统需要几毫秒。所以我们需要时间。我们将在这里的第一时刻。但是你可能想要一个晚上的时间而不是午夜来发出警报!
ZonedDateTime alarm = birthday.atStartOfDay();
Duration d = Duration.between( now , alarm );
long millis = d.toMillis();
大部分java.time功能都被反向移植到Java 6&amp; 7 ThreeTen-Backport项目。在ThreeTenABP项目中进一步适应Android。
不要将日期称为日期&#34;日期&#34;因为它含糊不清。按照java.time类中的命名进行操作。
不要将月份存储为英文名称。更容易在1月到12月存储1-12的整数。在Java代码中,使用Month
枚举对象而不是文本或数字。
答案 1 :(得分:0)
<div style="height: 100vh; background: greenyellow; width: 200px"></div>
<div style="height: 100vh; background: pink; width: 200px"></div>
您将上述值作为String(cursor.getString())获取,这些值是否定义为string或int?,您可以发布Create table query。
答案 2 :(得分:0)
您的共享代码中有很多混淆,例如:
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);
**您设置了很多值,但您的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;
}
所以我建议你分享People class full和table description。