2014-02-24 65 views
1

我創建的應用程序中的鳴叫的顯示列表我要顯示的時間像幾秒鐘前,1分鐘前,5M前,前1h ......昨天,最後的日期轉換日期爲字符串,如Twitter

我堅持了分秒幾秒鐘

public static String getTimeString(Date fromdate) { 

    long then; 
    then = fromdate.getTime(); 
    Date date = new Date(then); 

    StringBuffer dateStr = new StringBuffer(); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(date); 
    Calendar now = Calendar.getInstance(); 

    int days = daysBetween(calendar.getTime(), now.getTime()); 
    int minutes = hoursBetween(calendar.getTime(), now.getTime()); 
    int hours = minutes/60; 
    if (days == 0) { 

     int second = minuteBetween(calendar.getTime(), now.getTime()); 
     if (minutes > 60) { 

    } else 

    if (hours > 24 && days <= 7) { 
     dateStr.append(days).append("d"); 
    } else { 
     dateStr.append(twtimeformat.format(date)); 
    } 

    return dateStr.toString(); 
} 

public static int minuteBetween(Date d1, Date d2) { 
    return (int) ((d2.getTime() - d1.getTime())/DateUtils.SECOND_IN_MILLIS); 
} 

public static int hoursBetween(Date d1, Date d2) { 
    return (int) ((d2.getTime() - d1.getTime())/DateUtils.MINUTE_IN_MILLIS); 
} 

public static int daysBetween(Date d1, Date d2) { 
    return (int) ((d2.getTime() - d1.getTime())/DateUtils.DAY_IN_MILLIS); 
} 

回答

3

使用本

TextView time = (TextView) findViewById(R.id.time); 

getTimeDifference("your time", time); 

我在這裏是類getTimeDifference

private void getTimeDifference(String pDate, TextView time) { 
    int diffInDays = 0; 
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    format.setTimeZone(TimeZone.getTimeZone("GMT")); 
    Calendar c = Calendar.getInstance(); 
    String formattedDate = format.format(c.getTime()); 

    Date d1 = null; 
    Date d2 = null; 
    try { 

     d1 = format.parse(formattedDate); 
     d2 = format.parse(pDate); 
     long diff = d1.getTime() - d2.getTime(); 

     diffInDays = (int) (diff/(1000 * 60 * 60 * 24)); 
     if (diffInDays > 0) { 
      if (diffInDays == 1) { 
       time.setText(diffInDays + " day ago"); 
      } else { 
       time.setText(diffInDays + " days ago"); 
      } 
     } else { 
      int diffHours = (int) (diff/(60 * 60 * 1000)); 
      if (diffHours > 0) { 
       if (diffHours == 1) { 
        time.setText(diffHours + " hr ago"); 
       } else { 
        time.setText(diffHours + " hrs ago"); 
       } 
      } else { 

       int diffMinutes = (int) ((diff/(60 * 1000) % 60)); 
       if (diffMinutes == 1) { 
        time.setText(diffMinutes + " min ago"); 
       } else { 
        time.setText(diffMinutes + " mins ago"); 
       } 

      } 
     } 

    } catch (ParseException e) { 
     // System.out.println("Err: " + e); 
     e.printStackTrace(); 
    } 

} 
+0

謝謝@sishin – MilapTank

+0

你能幫助我的花葯同類問題? http://pastie.org/private/tjontopamvrngykrlv2dgg看到鏈接 – MilapTank

+0

好你需要那裏? – Sishin

0

日期到字符串轉換非常容易。請參閱下面的3行代碼。

// Create an instance of SimpleDateFormat used for formatting 
// the string representation of date (month/day/year) 
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 

// Get the date today using Calendar object. 
Date today = Calendar.getInstance().getTime();   
// Using DateFormat format method we can create a string 
// representation of a date with the defined format. 
String reportDate = df.format(today); 

// Print what date is today! 
System.out.println("Report Date: " + reportDate); 

如果您想傳遞自己的日期對象,然後在下面的代碼中傳遞它。

String reportDate = df.format(yourDateObject); 

// Print what date is today! 
System.out.println("Report Date: " + reportDate); 

感謝您的積極響應。

+0

謝謝你,但它不是我的答案我想只是像微博希望你明白我的問題 – MilapTank

0

看到這個解決方案

public static String getTWTimeString(Date fromdate) { 

long then; 
then = fromdate.getTime(); 
Date date = new Date(then); 

StringBuffer dateStr = new StringBuffer(); 

Calendar calendar = Calendar.getInstance(); 
calendar.setTime(date); 
Calendar now = Calendar.getInstance(); 

int days = daysBetween(calendar.getTime(), now.getTime()); 
int minutes = hoursBetween(calendar.getTime(), now.getTime()); 
int hours = minutes/60; 
if (days == 0) { 

    int second = minuteBetween(calendar.getTime(), now.getTime()); 
    if (minutes > 60) { 

     // dateStr.append(hours).append(hours > 1 ? "h" : "h"); 

     if (hours >= 1 && hours <= 24) { 
      dateStr.append(hours).append("h"); 
     } 

    } else { 

     if (second <= 10) { 
      dateStr.append("Now"); 
     } else if (second > 10 && second <= 30) { 
      dateStr.append("few seconds ago"); 
     } else if (second > 30 && second <= 60) { 
      dateStr.append(second).append("s"); 
     } else if (second >= 60 && minutes <= 60) { 
      dateStr.append(minutes).append("m"); 
     } 
    } 
} else 

if (hours > 24 && days <= 7) { 
    dateStr.append(days).append("d"); 
} else { 
    dateStr.append(twtimeformat.format(date)); 
} 

return dateStr.toString(); 
} 

public static int minuteBetween(Date d1, Date d2) { 
return (int) ((d2.getTime() - d1.getTime())/DateUtils.SECOND_IN_MILLIS); 
} 

public static int hoursBetween(Date d1, Date d2) { 
    return (int) ((d2.getTime() - d1.getTime())/DateUtils.MINUTE_IN_MILLIS); 
} 

public static int daysBetween(Date d1, Date d2) { 
    return (int) ((d2.getTime() - d1.getTime())/DateUtils.DAY_IN_MILLIS); 
}