Best way to format a date relative to now on Android

前端 未结 10 1691
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 15:57

I am creating a feature in an Android app to get an arbitrary date (past, present or future) and find the difference relative to now.

Both my now and

相关标签:
10条回答
  • 2021-01-31 16:49

    So your computation is based on milliseconds unit, then you format the result with SimpleDateFormat.

    For this, you can easily use SimpleDateFormat formatter like this :

    Date date = new Date(milliseconds);
    SimpleDateFormat formatter = new SimpleDateFormat("EEEE dd MMMM yyyy");
    String strDate = formatter.format(date);
    

    So your computation should be based on milliseconds unit, then you format the result with SimpleDateFormat.

    The pattern ("EEEE dd MMMM yyyy") allows you to get a date format like Monday, 04 February 2013.

    You can change the pattern as you like : "EEEE dd/MM/yy", ...

    0 讨论(0)
  • 2021-01-31 16:55

    Why not just check for yesterday and tomorrow to avoid the in 0 days/0 days ago bug and leave DateUtils.getRelativeTimeSpanString handle the remaining cases?

    String relative = null;
    
    if(now < due && (due-now)<864000){
        relative = "tomorrow";
    }else if(now > due && (now-due)<864000){
        relative = "yesterday";
    }else{
        relative = DateUtils.getRelativeTimeSpanString(due, now, DateUtils.DAY_IN_MILLIS); // e.g. "in 4 days"
    }
    
    Log.d("result", relative);
    

    Edit: You may also add today with a simple check as well.

    0 讨论(0)
  • 2021-01-31 16:56

    Finally I have implemented what you wanted..!

    First you need to download Joda Time from here

    Extract it to any folder and put joda-time-2.2.jar into androidProject/libs folder.

    MainActivity

    import org.joda.time.DateTime;
    import org.joda.time.Days;
    import org.joda.time.Months;
    import org.joda.time.MutableDateTime;
    import org.joda.time.Weeks;
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    
    
    public class MainActivity extends Activity
    {
      private int day ;
      private int month ;
      private int year ;
      private int hour ;
      private int minute ;
      private long selectedTimeInMillis;
      private long currentTimeInMillis;
      private String strDay ="";
    
     @Override
     protected void onCreate(Bundle savedInstanceState) 
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        year = 2013;
        month = 8;
        day = 10;
        hour = 15;
        minute = 28;
    
        DateTime selectedTime = new DateTime(year,month,day,hour,minute);
        selectedTimeInMillis = selectedTime.getMillis();
    
        MutableDateTime epoch = new MutableDateTime();
        epoch.setDate(selectedTimeInMillis); //Set to Epoch time
    
        DateTime now = new DateTime();
    
        currentTimeInMillis = now.getMillis();
    
        int days = Days.daysBetween(epoch, now).getDays();
        int weeks = Weeks.weeksBetween(epoch, now).getWeeks();
        int months = Months.monthsBetween(epoch, now).getMonths();
    
        Log.v("days since epoch: ",""+days);
        Log.v("weeks since epoch: ",""+weeks);
        Log.v("months since epoch: ",""+months);
    
    
        if(selectedTimeInMillis < currentTimeInMillis) //Past 
        {       
            long yesterdayTimeInMillis = currentTimeInMillis - 86400000;
    
            DateTime today = new DateTime(currentTimeInMillis);
            int year = today.getDayOfYear();
            int intToday = today.getDayOfMonth();
            DateTime yesterday = new DateTime(yesterdayTimeInMillis);
            int intYesterday = yesterday.getDayOfMonth();
            DateTime selectedDay = new DateTime(selectedTimeInMillis);
            int intselectedDay = selectedDay.getDayOfMonth();
            int intselectedYear = selectedDay.getDayOfYear();
    
            if(intToday == intselectedDay & year == intselectedYear)
            {
                strDay = "today";
            }
            else if(intYesterday == intselectedDay)
            {
                strDay = "yesterday";
            }
            else
            {
                strDay = "before "+ days +" days from today";
            }
    
    
        }
        else if(selectedTimeInMillis > currentTimeInMillis) //Future
        {
            long tomorrowTimeInMillis = currentTimeInMillis + 86400000;
    
            DateTime tomorrow = new DateTime(tomorrowTimeInMillis);
            int intTomorrow = tomorrow.getDayOfMonth();
            DateTime today = new DateTime(selectedTimeInMillis);
            int intToday = today.getDayOfMonth();
    
            if(intToday == intTomorrow)
            {
                strDay = "tomorrow";
            }
            else
            {
                days = -days;
                strDay = "after "+ days +" days from today";
            }
    
    
        }
    
        Log.v("strDay: ",""+strDay);
      }
    }
    

    You just need to change the value of day and you will get the desire output. Currently I have given date 10 as input so output will be today.

    I have set date/day = 10 , month = 8 , year = 2013 , hour = 15 , min = 28

    For past dates:

    input day 9 output yesterday
    
    input day 3 output before 7 days from today
    
    input year 2012 and day 10 output before 365 days from today
    

    For future dates:

    input day 11 output tomorrow
    
    input day 27 output after 17 days from today
    
    input day 23 and year 2016 output after 1109 days from today
    
    0 讨论(0)
  • 2021-01-31 16:56

    long now = System.currentTimeMillis();

    DateUtils.getRelativeDateTimeString(mContext, now), DateUtils.SECOND_IN_MILLIS, DateUtils.DAY_IN_MILLIS, 0)

    a link!

    0 讨论(0)
提交回复
热议问题