DateUtils.getRelativeTimeSpanString for future dates

前端 未结 3 1947
北荒
北荒 2021-01-14 14:28

So [DateUtils.getRelativeTimeSpanString()][1] in the Android SDK works great for showing relative times that are in the past.

i.e: 5 days ago, or 5 minutes ago.

相关标签:
3条回答
  • 2021-01-14 15:08

    I'd try using DateUtils.getRelativeTimeSpanString(time, now, minResolution) as it works for future dates using an "in n days" format.

    http://developer.android.com/reference/android/text/format/DateUtils.html#getRelativeTimeSpanString

    0 讨论(0)
  • 2021-01-14 15:13
    DateUtils.getRelativeTimeSpanString(endDate.getTime(), startDate.getTime(), DateUtils.FORMAT_ABBREV_RELATIVE);
    

    With the following caveat:

    Gives tomorrow instead of in 1 day.

    0 讨论(0)
  • 2021-01-14 15:27

    Follow following steps

    String dateString = "06/05/2019 06:49:00 AM";
    
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    
     Date convertedDate = new Date();
    try {
          convertedDate = dateFormat.parse(dateString);
          long cuMillis = System.currentTimeMillis();
    
          String timeAgo = (String) DateUtils.getRelativeTimeSpanString(convertedDate.getTime(), cuMillis, 1, FORMAT_ABBREV_RELATIVE);
    
          Log.d("LOG", "Time Ago==>" + timeAgo);
    
       } catch (ParseException e) {
    
        e.printStackTrace();
      } 
    
    0 讨论(0)
提交回复
热议问题