0

I am using Following codes in C# to display text like 2 DAYS AGO, 43 MINUTES AGO , 8 DAYS AGO and so on depending on the DateTime object in C#. The DateTime Object is in format of " YYYY-MM-DDThh-mm-ss ". But I am not getting any such feature in case of Android. The following is the code snippet I a using in C# . Can any one tell me which class and which Date Formatter can we use in case of android. Please share the code with me. Thank you in advance.

/// <param name="dtObject"></param>
/// <returns></returns>
public static string FormatDateTimeToDisplay(DateTime dtObject)
{
    TimeSpan tsObject = DateTime.Now.Subtract(dtObject);
    string formatedTime = string.Empty;

    //Code to format the text to add number of minutes
    if ((tsObject.Days <= 0) && (tsObject.Hours <= 0) && (tsObject.Minutes < 2))
    {
        formatedTime = tsObject.Minutes + " minute ago";
    }

    else if ((tsObject.Days <= 0) && (tsObject.Hours <= 0) && (tsObject.Minutes >= 2))
    {
        formatedTime = tsObject.Minutes + " minutes ago";
    }

    //Code to format the text to add number of hours
    else if ((tsObject.Days <= 0) && (tsObject.Hours < 2))
    {
        formatedTime = tsObject.Hours + " hour " + tsObject.Minutes + " minutes ago";
    }

    //Code to format the text to add number of hours and minutes
    else if ((tsObject.Days <= 0) && (tsObject.Hours >= 2))
    {
        formatedTime = tsObject.Hours + " hours " + tsObject.Minutes + " minutes ago";
    }

    //Code to format the text to add number of days
    else if ((tsObject.Days < 2))
    {
        formatedTime = tsObject.Days + " day ago";
    }

    else if ((tsObject.Days >= 2) && (tsObject.Days <= 15))
    {
        formatedTime = tsObject.Days + " days ago";
    }

    //Code to format the text to add the exact date
    else
    {
        formatedTime = "on " + dtObject.ToString("MM/dd/yyyy") + "";
    }

    return "(" + formatedTime + ")";
}
  • A `DateTime` object does not have a format. Are you asking some to convert this code to java for you? – Ben Robinson Dec 04 '14 at 12:51
  • I know it has no format... I am getting a json String data as "2014-10-29T16:48:09" which is working fine with C# codes but I am not able to get the same output in java. If u have code then please share. – Rahul Ravi Dec 04 '14 at 12:54
  • THe code you have posted does not feature any json strings, so how is it relevant to the question? – Ben Robinson Dec 04 '14 at 12:55
  • after extracting data from the json ... i am getting that string... And i have to make use of that string.. and display the desired result. Parsing is already done. just i need to make use of "2014-10-29T16:48:09" – Rahul Ravi Dec 04 '14 at 12:57
  • Related? [How do I calculate relative time?](http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time) – Soner Gönül Dec 04 '14 at 13:02

2 Answers2

0

IMHO,DateUtils.getRelativeTimeSpanString may help you.

  String time = "2014-12-01T16:48:09";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
  Date date = sdf.parse(time);
  String str = (String) DateUtils.getRelativeDateTimeString(this, date.getTime(), 
      1000, //minResolution
      24 * 60 * 60 * 1000, //transitionResolution 
      0);
  // split the string using comma if you want
  str = str.substring(0, str.indexOf(","));

You can see more here Android: getRelativeTime example

Community
  • 1
  • 1
Jerome
  • 1,749
  • 1
  • 14
  • 40
0

You can adapt the following method to match your needs (note that it takes a Unix-timestamp as parameter):

   public static String formatLastOnline(long time, Context c) {
    Calendar calendarFromGivenTime = Calendar.getInstance(Locale.getDefault());
    calendarFromGivenTime.setTimeInMillis(time * 1000);
    Calendar calendarFromNow = Calendar.getInstance(Locale.getDefault());
    calendarFromNow.add(Calendar.DAY_OF_YEAR, -1);

    if (DateUtils.isToday(time * 1000))
        return c.getString(R.string.today_at) + " " + DateFormat.format("HH:mm", calendarFromGivenTime).toString();
    else if (calendarFromNow.get(Calendar.YEAR) == calendarFromGivenTime.get(Calendar.YEAR)
            && calendarFromNow.get(Calendar.DAY_OF_YEAR) == calendarFromGivenTime.get(Calendar.DAY_OF_YEAR)) {
        return c.getString(R.string.yesterday_at) + " " + DateFormat.format("HH:mm", calendarFromGivenTime).toString();
    }
    String ddMMyyyy = DateFormat.format("dd.MM.yyyy", calendarFromGivenTime).toString();
    String HHmm = DateFormat.format("HH:mm", calendarFromGivenTime).toString();
    return ddMMyyyy + " " + c.getString(R.string.at) + " " + HHmm;
}
Droidman
  • 11,485
  • 17
  • 93
  • 141