Search code examples
androiddatetimeandroid-date

Unparseable date: "10:40:54" (at offset 0)


I have time in millis since 1970, i am converting it to string formated as this "EEEE-yyyy/MM/dd-hh:mm:ss", then split it to array, and trying to parse hh:mm:ss part to date, so i cuold use date.compareTo method, but it gives me unparseable date exception.

public class NotificationWakefulIntentService extends IntentService {

    public NotificationWakefulIntentService() {
        super("NotificationWakefulIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("time",(System.currentTimeMillis()/1000)+"");

        Date dateTemp = null;
        Date dateTempLocal = null;
        List<Action> actionList = Action.listAll(Action.class);



        for (Action act: actionList){
            if (act.isRepeating()){
                SimpleDateFormat formatter = new SimpleDateFormat("EEEE-yyyy/MM/dd-hh:mm:ss", Locale.getDefault());
                String asString = formatter.format(act.getDate());
                String[] parsedDate = asString.split("-");
                Log.e("time", asString);

                String time = parsedDate[2];
                //String date = parsedDate[1];
                String day = parsedDate[0];

                String asStringLocal = formatter.format(System.currentTimeMillis());
                String[] parsedDateLocal = asStringLocal.split("-");

                String timeLocal = parsedDateLocal[2];
                //String dateLocal = parsedDateLocal[1];
                String dayLocal = parsedDateLocal[0];

                try {
                    dateTemp = formatter.parse(time);
                    dateTempLocal = formatter.parse(timeLocal);
                    Log.e("dateEquality", (dateTemp.compareTo(dateTempLocal))+"");
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                if (dateTemp!=null && dateTempLocal!=null) {
                    if (day.equals(dayLocal) && dateTemp.compareTo(dateTempLocal) > 0) {
                        Log.e("notify", "notify");
                    }
                }
            }else {

            }
        }

        SimpleWakefulReciever.completeWakefulIntent(intent);
    }
}

LogCat and data log

E/time: понедельник-2016/10/24-10:40:54
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: java.text.ParseException: Unparseable date: "10:40:54" (at offset 0)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err:     at java.text.DateFormat.parse(DateFormat.java:626)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err:     at by.mtz.reminder.wakefulService.NotificationWakefulIntentService.onHandleIntent(NotificationWakefulIntentService.java:58)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err:     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err:     at android.os.Looper.loop(Looper.java:137)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:60)
10-24 09:27:06.805 2655-3407/by.mtz.reminder D/time: 1477290426

Solution

  • Your format string is false. It must be: new SimpleDateFormat("hh:mm:ss"); for parsing the time part only.

    So in your code you need one for parsing the time and one for formatting your string