我正在使用DateUtils.getrelativeTimeSpanString()来显示帖子.
它返回的字符串是这样的:1分钟前,但我希望是1米或1小时等等(缩短和没有前).
我可以将“小时”替换为“h”或“分钟”替换为“m”,但如果语言与英语不同则不起作用.这就是Twitter现在正在使用的东西.英语/西里尔语通过Twitter显示的方式.
更新:我会接受@Bradley Wilson的回答,虽然我会在这里添加更清洁的解决方案(再次使用JodaTime包).其余的答案也可以修改为相同的结果,所以他们值得投票.谢谢你们:
DateTime postMaded = new DateTime(your prevIoUs date); DateTime NowUpdate = new DateTime(); Period period = new Period(postMaded, NowUpdate); PeriodFormatter formatter; Locale current = ConverterMethods.getCurrentLocale(); if (current.getLanguage().contentEquals(any Cyrillic language )) { if (period.getYears() != 0) { formatter = new PeriodFormatterBuilder().appendYears().appendSuffix(" г.").printZeroNever().toFormatter(); } else if (period.getMonths() != 0) { formatter = new PeriodFormatterBuilder().appendMonths().appendSuffix(" м").printZeroNever().toFormatter(); } else if (period.getWeeks() != 0) { formatter = new PeriodFormatterBuilder().appenDWeeks().appendSuffix(" седм.").printZeroNever().toFormatter(); } else if (period.getDays() != 0) { formatter = new PeriodFormatterBuilder().appendDays().appendSuffix(" д").printZeroNever().toFormatter(); } else if (period.getHours() != 0) { formatter = new PeriodFormatterBuilder().appendHours().appendSuffix(" ч").printZeroNever().toFormatter(); } else if (period.getMinutes() != 0) { formatter = new PeriodFormatterBuilder().appendMinutes().appendSuffix(" мин").printZeroNever().toFormatter(); } else { formatter = new PeriodFormatterBuilder().appendSeconds().appendSuffix(" с").printZeroNever().toFormatter(); } } else { if (period.getYears() != 0) { formatter = new PeriodFormatterBuilder().appendYears().appendSuffix(" y").printZeroNever().toFormatter(); } else if (period.getMonths() != 0) { formatter = new PeriodFormatterBuilder().appendMonths().appendSuffix(" mon").printZeroNever().toFormatter(); } else if (period.getWeeks() != 0) { formatter = new PeriodFormatterBuilder().appenDWeeks().appendSuffix(" w").printZeroNever().toFormatter(); } else if (period.getDays() != 0) { formatter = new PeriodFormatterBuilder().appendDays().appendSuffix(" d").printZeroNever().toFormatter(); } else if (period.getHours() != 0) { formatter = new PeriodFormatterBuilder().appendHours().appendSuffix(" h").printZeroNever().toFormatter(); } else if (period.getMinutes() != 0) { formatter = new PeriodFormatterBuilder().appendMinutes().appendSuffix(" m").printZeroNever().toFormatter(); } else { formatter = new PeriodFormatterBuilder().appendSeconds().appendSuffix(" s").printZeroNever().toFormatter(); } }
解决方法:
您可以使用JodaTime来实现此目的:
将依赖项添加到您的应用程序级Build.Gradle文件,如下所示:
compile 'net.danlew:androID.joda:2.9.4.1'
然后你可以简单地实现这个功能.
private String time = "Just Now";private String how_long_ago(String created_at) { DateTime sinceGraduation = new DateTime(created_at, GregorianChronology.getInstance()); DateTime currentDate = new DateTime(); //current date Months diffInMonths = Months.monthsBetween(sinceGraduation, currentDate); Days diffInDays = Days.daysBetween(sinceGraduation, currentDate); Hours diffInHours = Hours.hoursBetween(sinceGraduation, currentDate); Minutes diffInMinutes = Minutes.minutesBetween(sinceGraduation, currentDate); Seconds seconds = Seconds.secondsBetween(sinceGraduation, currentDate); Log.d("since grad", "before if " + sinceGraduation); if (diffInDays.isGreaterThan(Days.days(31))) { time = diffInMonths.getMonths() + " months ago"; if (diffInMonths.getMonths() == 1) { time = diffInMonths.getMonths() + " month ago"; } else { time = diffInMonths.getMonths() + " months ago"; } return time; } else if (diffInHours.isGreaterThan(Hours.hours(24))) { if (diffInDays.getDays() == 1) { time = diffInDays.getDays() + " day ago"; } else { time = diffInDays.getDays() + " days ago"; } return time; } else if (diffInMinutes.isGreaterThan(Minutes.minutes(60))) { if (diffInHours.getHours() == 1) { time = diffInHours.getHours() + " hour ago"; } else { time = diffInHours.getHours() + " hours ago"; } return time; } else if (seconds.isGreaterThan(Seconds.seconds(60))) { if (diffInMinutes.getMinutes() == 1) { time = diffInMinutes.getMinutes() + " minute ago"; } else { time = diffInMinutes.getMinutes() + " minutes ago"; } return time; } else if (seconds.isLessthan(Seconds.seconds(60))) { return time; } Log.d("since grad", "" + sinceGraduation); return time; }
只需将String输出更改为时间即可达到所需的’1m’
注意:您可以使用本地化来更改资源中的字符串.
有关详细信息,请参阅Android Localization Tutorial.
总结以上是内存溢出为你收集整理的java – 显示时间前的Twitter全部内容,希望文章能够帮你解决java – 显示时间前的Twitter所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)