DateUtils.getRelativeTimeSpanString for future dates

前端 未结 3 1949
北荒
北荒 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: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();
      } 
    

提交回复
热议问题