本文介绍了相对时间范围的自定义格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用: CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString( dateObject.getTime(), System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS,标志); 这将返回相对时间跨度: 1分钟前 36分钟前 4小时前 ... 我要做的是显示这个,像这样: 1m 36m 4h ... 我知道DateUtils有FORMAT_ABBREV_ALL标志,但这并不缩写字符串,想要... 如何创建自定义的东西?解决方案我已经在我的twitter模块中完成了 public static String getTimeString(Date fromdate){ 长 then = fromdate.getTime(); Date date = new Date(then); StringBuffer dateStr = new StringBuffer(); 日历calendar = Calendar.getInstance(); calendar.setTime(date); 现在日历= Calendar.getInstance(); int days = dayBetween(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){ if(hours> = 1&&小时< = 24){ dateStr.append(hours).append (H); } } else { if(second< = 10){ dateStr.append(Now); } else if(second> 10&& second< = 30){ dateStr.append(几秒前); } else if(second> 30&& second< = 60){ dateStr.append(second).append(s); } else if(second> = 60&&分钟 dateStr.append(minutes).append(m); } } } else if(hours> 24&& days< = 7){ dateStr.append ).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); } I'm trying to create an custom format to display the elapsed time.Now I'm using:CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString( dateObject.getTime(), System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS, flags);This returns relative time spans like this:1 minute ago36 minutes ago4 hours ago...What I'm trying to do is display this, like this:1m36m4h...I know DateUtils has FORMAT_ABBREV_ALL flag, but this doesn't abbreviate the string like I want to...How can I create something custom for this ? 解决方案 i have done this in my twitter module 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) { 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);} 这篇关于相对时间范围的自定义格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-09 10:13