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
}
}