Question

I am trying to compare a date I retrieved from a MySQL with the current Date/Time.

The desired effect I want is this:

Date is Recent (Less than an hour)               | 0
Date is An Hour Ago (1-2 Hours before now)       | 1
Date is Several Hours Ago (2-8 Hours before now) | 2
Date is Yesterday (a day ago)                    | 3
Date is Recent (More than a day ago)             | 4

To explain: The method will return a number between 0-4 (inclusive) as a choice.

I currently have a mangled mess of code below, but maybe someone can suggest a better method?

compareDate method:

private int compareDate(Date date1)
{
    long daysAgo = getDaysAgo(date1);

    if(daysAgo == 0)
    {
        //Check the hours

        int choice = getMinAgo(date1);

        //Code for "Minutes Ago"
        if(choice == 0)
        {
           return 0;
        }

        //Code for "An Hour Ago"
        else if(choice == 1)
        {
            return 1;
        }

        //Code for "Several Hours Ago"
        else if(choice == 2)
        {
            return 2;
        }

        else return -1;

    }

    else if (daysAgo == 1)
    {
        //"Code" for "Yesterday" is 3
        return 3;
    }

    else
    {
        //"Code" for "Several days ago" is 4
        return 4;
    }
}

getDaysAgo method

private long getDaysAgo(Date date)
{
    long days = (new Date().getTime() - date.getTime()) / DAYS_IN_MILLISEC;

    return days;
}

getMinAgo method

private int getMinAgo(Date date)
{
    long hourAgo = 60;
    long twoHourAgo = hourAgo*2;
    long eightHourAgo = hourAgo*8;

    Date dateHourAgo = new Date(date.getTime() - hourAgo*60*1000);
    Date dateTwoHourAgo = new Date(date.getTime() - twoHourAgo*60*1000);
    Date dateEightHourAgo = new Date(date.getTime() - eightHourAgo*60*1000);

    //Between 0-1 Hours Ago
    if(date.before(dateHourAgo))
    {
        return 0;
    }

    //Between 1-2 Hours Ago
    else if(date.after(dateHourAgo) && date.before(dateTwoHourAgo))
    {
        return 1;
    }

    else if(date.after(dateTwoHourAgo) && date.before(dateEightHourAgo))
    {
        return 2;
    }

    return -1;
}
Was it helpful?

Solution

The code below is an example how you can implement the method ( I named it getChoice ). I have tested it and works ok. If something is unclear don't hesitate and ask.

import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Date testedDate = new Date(114, 4, 10, 22, 0, 0);
        TextView tv = (TextView)findViewById(R.id.textView1);

        int i = getChoice(testedDate);
        String mOutput = String.valueOf(i);

        tv.setText("The output is: " + mOutput);

    }

    private int getChoice(Date d) {
        long mTime = new Date().getTime() - d.getTime();

        if(mTime <= 3600000) return 0; // less than an hour
        if(mTime <= 7200000) return 1; // 1-2 hours before now
        if(mTime <= 28800000) return 2; // 2-8 hours before now
        if(mTime <= 86400000) return 3; // a day ago
        return 4; // more than a day ago
    }

}

OTHER TIPS

I would return the integer value from MySQL, rather than writing a lot of code.

(It's not clear what you are intending to return if the date value is in the future, or if the date is NULL.)

SELECT t.datetime_col
     , CASE
       WHEN t.datetime_col > NOW() THEN -1                     -- in the future 
       WHEN t.datetime_col >= NOW() - INTERVAL  1 HOUR THEN 0  -- within the last hour
       WHEN t.datetime_col >= NOW() - INTERVAL  2 HOUR THEN 1  -- within the last two hours 
       WHEN t.datetime_col >= NOW() - INTERVAL  8 HOUR THEN 2  -- within the last eight hours
       WHEN t.datetime_col >= NOW() - INTERVAL 24 HOUR THEN 3  -- within the past day
       WHEN t.datetime_col <  NOW() - INTERVAL 24 HOUR THEN 4  -- older than a day 
       END AS recent
     , ...
  FROM ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top