vendredi 11 septembre 2015

DateTime to elapsedTime conversion causes crash and is inaccurate

So I have been trying to figure out how to get the whole "posted moments ago.", "posted 3 hours ago.", "posted a day ago" algorythm down and for some reason when I run this code it causes the program to randomly crash. It also is completely inaccurate for anything after a day. How can I improve my code to the simplest form to accomplish my goal of accurately posting elapsed time from a datetime stored in a database without the program crashing and all?

public String getNotificationElapsedTime(final int notificationID){
    final String[] timeElapsed = {"moments ago."};
    runnable = new Runnable() {
        @Override
        public void run() {
            openToRead();

            String Query = "SELECT * FROM " + Constants.TABLE_NOTIFICATION + " WHERE " + Constants.KEY_NOTIFICATION_ID + "=" + notificationID;
            Cursor cursor = mSqLiteDatabase.rawQuery(Query, null);
            if(cursor.moveToFirst()){
                while (cursor.isAfterLast() == false) {
                    try {
                        if(cursor.getInt(cursor.getColumnIndex(Constants.KEY_NOTIFICATION_ID)) == notificationID) {
                            int indexNotificationStatus = cursor.getColumnIndex(Constants.KEY_NOTIFICATION_TIME);
                            String timeCreated = cursor.getString(indexNotificationStatus);

                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Date currentDate = new Date();
                            Date notificationDate = new Date();

                            try {
                                notificationDate = simpleDateFormat.parse(timeCreated.trim());
                            } catch(ParseException e){

                            }
                            long milliElapsed = currentDate.getTime() - notificationDate.getTime() + 14400000;
                            if(milliElapsed>=60000){
                                long minutesElapsed = milliElapsed/60000;
                                timeElapsed[0] = minutesElapsed + " minutes ago.";
                                if(minutesElapsed>=60){
                                    long hoursElapsed = minutesElapsed/60;
                                    timeElapsed[0] = hoursElapsed + " hours ago.";
                                    if(hoursElapsed>=24){
                                        long daysElapsed = hoursElapsed/60;
                                        timeElapsed[0] = daysElapsed + " days ago.";
                                        if(daysElapsed>=7){
                                            long weeksElapsed = daysElapsed/7;
                                            timeElapsed[0] = hoursElapsed + " weeks ago.";
                                            if(weeksElapsed>=4){
                                                long monthsElapsed = weeksElapsed/4;
                                                timeElapsed[0] = monthsElapsed + " months ago.";
                                                if(daysElapsed>=365){
                                                    long yearsElapsed = daysElapsed/365;
                                                    timeElapsed[0] = yearsElapsed + " years ago.";
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        exceptionHandler.alert(e, "getNotificationElapsedTime()");
                    }
                    cursor.moveToNext();
                }
            }
            close();
            if (null != sqLiteDbQueryListener) {
                ((Activity) mContext).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        hideProgressDialogue();
                        //sqLiteDbQueryListener.onSuccess(reqCode);
                    }
                });
                return;
            }
        }
    };
    new Thread(runnable).start();
    return timeElapsed[0];
}

Aucun commentaire:

Enregistrer un commentaire