2014-12-04 85 views
0

我在C#中使用以下代码来显示文本,例如2天AGO,43分钟AGO,8天AGO等等,具体取决于在C#中的DateTime对象。 DateTime对象格式为“YYYY-MM-DDThh-mm-ss”。但是我没有在Android上获得这样的功能。以下是我在C#中使用的代码片段。任何人都可以告诉我哪个类和哪个日期格式化程序可以用于android的情况下。请与我分享代码。先谢谢你。android代码从“YYYY-MM-DDThh-mm-ss”格式化日期和时间

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

了'DateTime'对象没有格式。你问一些为你转换这段代码为java吗? – 2014-12-04 12:51:37

+0

我知道它没有格式......我得到一个JSON字符串数据为“2014-10-29T16:48:09”,它与C#代码正常工作,但我无法在Java中获得相同的输出。如果你有代码,请分享。 – 2014-12-04 12:54:03

+0

您发布的代码不包含任何json字符串,那么它与问题的关系如何? – 2014-12-04 12:55:18

回答

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

+0

请详细说明 – 2014-12-04 12:54:28

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