我是android新手。我将时间存储在格式为hh:mm的字符串中,并希望将其与当前系统时间进行比较。如果匹配我想要生成通知。 这是代码比较一个包含时间的字符串与系统时间android
注意: THE STRING str保留时间。
time = days+1;
while(i>0)
{
long hour=calendar.get(calendar.HOUR_OF_DAY);
long min=calendar.get(calendar.MINUTE);
String str = alarm[time];
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm");
try {
Date date = (Date)formatter.parse(str);
Calendar c2 = GregorianCalendar.getInstance(); // creates a new calendar instance
c2.setTime(date); // assigns calendar to given date
hours=c2.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
minutes=c2.get(Calendar.MINUTE);
if(hours==hour && min==minutes)
{
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_books)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker(res.getString(R.string.Cal))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle("You Have a Lecture !!")
.setContentText(alarm[time+1]);
Notification n = builder.build();
nm.notify(0, n);
}
i--;
time=time+days;
} catch (java.text.ParseException e) {
e.printStackTrace();
}
}
,但我得到一个
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
请帮助。
感谢您的帮助:) – Ruchika