私はそれが機能していると思います... リストビューがあるアクティビティでこのメソッドを使用し、onCreate、onStart、および onResume ごとに呼び出します。
public void setRelevantDate() {
SimpleDateFormat year = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
Date date = new Date();
String current_date = year.format(date);
String db_day_after_creation_date;
//check dates created, sub "today" or "yesterday"
for(SoundData s: app.unsent_recordings){
Log.e("soundData date flag",String.valueOf(s.isAfter_Day_two()));
Log.e("soundData date buffer",s.getDate_buffer());
Log.e("soundData date created",s.getDate_created());
if (!s.isAfter_Day_two()){ //if we are two days after the creation then we can skip all the checks for this item because
// it has already been set back to the initial MM/dd/yyyy date format
String sql_date = s.getDate_buffer();
String db_date = sql_date.substring(0,10);
String db_time = sql_date.substring(11);
//find the day after creation date
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(db_date));
} catch (ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DATE, 1);
db_day_after_creation_date = sdf.format(c.getTime());
if (db_date.equals(current_date)){
s.setDate_created("Today, "+db_time);
app.update_Recording_DateCreated(s.getId(),"Today, "+db_time, s.getDate_buffer(), String.valueOf(s.isAfter_Day_two())); //dateCreated = today, TIME
}
else if(current_date.equals(db_day_after_creation_date)){ // basically if the current date is equal to the day after the db_created date
s.setDate_created("Yesterday, "+db_time);
//we can say that it was created yesterday
app.update_Recording_DateCreated(s.getId(),"Yesterday, "+db_time, s.getDate_buffer(), String.valueOf(s.isAfter_Day_two()));
}
else{
s.setDate_created(sql_date);
//Otherwise use normal date
app.update_Recording_DateCreated(s.getId(),sql_date,s.getDate_buffer(), String.valueOf(s.isAfter_Day_two())); //dateCreated = dateBuffer
}
app.mCursor = app.db.getCursor();
app.rec_adapter.changeCursor(app.mCursor);
app.rec_adapter.notifyDataSetChanged(); //Update cursor, notifyDataSetChanged()
}