从“YYYY-MM-DDThh-mm-ss”格式化日期和时间的android代码

时间:2014-12-04 12:48:03

标签: c# android date datetime

我在C#中使用以下代码来显示文本,例如2 DAYS AGO,43 MINUTES AGO,8 DAYS AGO等等,具体取决于C#中的DateTime对象。 DateTime对象的格式为“YYYY-MM-DDThh-mm-ss”。但是在Android的情况下我没有得到任何这样的功能。以下是我在C#中使用的代码片段。任何人都可以告诉我哪个类和哪个Date Formatter可以在android的情况下使用。请与我分享代码。提前谢谢。

/// <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 + ")";
}

2 个答案:

答案 0 :(得分:0)

恕我直言,DateUtils.getRelativeTimeSpanString可以帮助你。

  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(","));

您可以在此处查看更多内容Android: getRelativeTime example

答案 1 :(得分:0)

您可以调整以下方法以满足您的需求(请注意,它需要将Unix时间戳作为参数):

   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;
}