Pergunta

I am new to android. I am storing time in a string in the format hh:mm and want to compare it with current system time. if the match i want to generate a notification. This is the code

NOTE: THE STRING str holds time.

     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();
            }
        }

but i get a

java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

please help.

Foi útil?

Solução 2

java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

this line

(Date)formatter.parse(str)

returns an java.util.Date object, but you are casting it to java.sql.Date. Check your import and adjust it (change from java.sql.Date to java.util.Date)

Outras dicas

You have imported the below package...

 import java.sql.Date;

You should import the package as below...So, you should remove the above import and import this below one.

 import java.util.Date;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top