我是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