用一个数字显示相对时间和Joda时间[Java]
因此,我使用并希望它只显示一个数字(我知道如何在Joda中处理多元化,最后只是一个简单的例子) 示例:用一个数字显示相对时间和Joda时间[Java],java,jodatime,duration,period,Java,Jodatime,Duration,Period,因此,我使用并希望它只显示一个数字(我知道如何在Joda中处理多元化,最后只是一个简单的例子) 示例: 2年2个月2周前应该只显示2年前 2天2小时2分钟前应该只显示2天前 我正在寻找一种方法,当它得到一个非零的数字时,可以使附件短路。我已经搜索并找到了其他框架的示例,但我们已经在使用Joda time,如果可能的话,我不想删除其他依赖项 我现在掌握的代码如下: protected final String toRelativeTime(final DateTime dateTime) {
2年2个月2周前
应该只显示2年前
2天2小时2分钟前
应该只显示2天前
我正在寻找一种方法,当它得到一个非零的数字时,可以使附件短路。我已经搜索并找到了其他框架的示例,但我们已经在使用Joda time,如果可能的话,我不想删除其他依赖项
我现在掌握的代码如下:
protected final String toRelativeTime(final DateTime dateTime) {
DateTime now = new DateTime();
Period period = new Period(dateTime, now);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendYears().appendSuffix(" years")
.appendMonths().appendSuffix(" months")
.appendWeeks().appendSuffix(" weeks")
.appendDays().appendSuffix(" days")
.appendHours().appendSuffix(" hours")
.appendMinutes().appendSuffix(" minutes")
.appendSeconds().appendSuffix(" seconds")
.printZeroNever()
.toFormatter();
return formatter.print(period);
}
下面的功能将完全满足您的要求
protected final String toRelativeTime(final DateTime dateTime) {
long nowLngTime = System.currentTimeMillis();
DateTime now = new DateTime();
long difference = Math.abs(dateTime.getTime() - nowLngTime);
Period period = new Period(dateTime, now);
PeriodFormatterBuilder formatterBuilder = new PeriodFormatterBuilder();
if (difference > DateUtils.YEAR_IN_MILLIS) {
formatterBuilder.appendYears().appendSuffix(" year");
} else if (difference > DateUtils.DAY_IN_MILLIS * 30) {
formatterBuilder.appendMonths().appendSuffix(" month");
} else if (difference > DateUtils.WEEK_IN_MILLIS) {
formatterBuilder.appendWeeks().appendSuffix(" week");
} else if (difference > DateUtils.DAY_IN_MILLIS) {
formatterBuilder.appendDays().appendSuffix(" day");
} else if (difference > DateUtils.HOUR_IN_MILLIS) {
formatterBuilder.appendHours().appendSuffix(" hour");
} else if (difference > DateUtils.MINUTE_IN_MILLIS) {
formatterBuilder.appendMinutes().appendSuffix(" minute");
} else if (difference > DateUtils.SECOND_IN_MILLIS) {
formatterBuilder.appendSeconds().appendSuffix(" second");
}
String ends = formatterBuilder.printZeroNever().toFormatter().print(period);
String plural = ends.startsWith("1 ")?"":"s";
return plural;
}
最后我做了这个
private final String toRelativeTime(LocalDateTime startTime, LocalDateTime endTime) {
final String sufix = " ago";
final Long seconds = ChronoUnit.SECONDS.between(startTime, endTime);
final Long minutes = ChronoUnit.MINUTES.between(startTime, endTime);
final Long hours = ChronoUnit.HOURS.between(startTime, endTime);
final Long days = ChronoUnit.DAYS.between(startTime, endTime);
final Long weeks = ChronoUnit.WEEKS.between(startTime, endTime);
final Long months = ChronoUnit.MONTHS.between(startTime, endTime);
final Long years = ChronoUnit.YEARS.between(startTime, endTime);
if(years > 0) {
return years.toString() + " " + checkPluralisation(years, "year", "years") + sufix;
} else if(months > 0) {
return months.toString() + " " + checkPluralisation(months, "month", "months") + sufix;
}else if(weeks > 0) {
return weeks.toString() + " " + checkPluralisation(weeks, "week", "weeks") + sufix;
}else if(days > 0) {
return days.toString() + " " + checkPluralisation(days, "day", "days") + sufix;
}else if(hours > 0) {
return hours.toString() + " " + checkPluralisation(hours, "hour", "hours") + sufix;
}else if(minutes > 0) {
return minutes.toString() + " " + checkPluralisation(minutes, "minute", "minutes") + sufix;
} else {
return seconds.toString() + " " + checkPluralisation(seconds, "second", "seconds") + sufix;
}
}
您还搜索了哪些其他框架?我只是好奇。正如您在建议的答案中所看到的,Joda Time在这方面处理起来相当笨拙。还有很多类似于Joda Time的工作,而不是一行;-)无论如何,您的回答表明Joda Time没有提供足够的功能来停止java-8中(官方推荐的)到java.Time的迁移。至少您的(仍然很长)解决方案与Joda Time中的工作大致相同,并且可以向愿意迁移的人展示如何替换丢失的
PeriodFormatter
,因此我向上投票。@MenoHochschild同意,这感觉有点长。在Joda时代,我并没有把它变小,所以我只使用Java8。