我有以下方法:
public static String formatDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Calendar today = Calendar.getInstance();
Calendar yesterday = Calendar.getInstance();
yesterday.add(Calendar.DATE, -1);
DateFormat timeFormatter = new SimpleDateFormat("hh:mma");
DateFormat dateTimeFormatter = new SimpleDateFormat("MMM d, yyyy - hh:mma");
if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) && calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR) && calendar.get(Calendar.HOUR_OF_DAY) == today.get(Calendar.HOUR_OF_DAY) && calendar.get(Calendar.MINUTE) == today.get(Calendar.MINUTE)) {
return "few seconds ago ";
} else if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) && calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR) && calendar.get(Calendar.HOUR_OF_DAY) == today.get(Calendar.HOUR_OF_DAY)) {
return "few minutes ago ";
} else if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) && calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) {
return "today " + timeFormatter.format(date);
} else if (calendar.get(Calendar.YEAR) == yesterday.get(Calendar.YEAR) && calendar.get(Calendar.DAY_OF_YEAR) == yesterday.get(Calendar.DAY_OF_YEAR)) {
return "yesterday " + timeFormatter.format(date);
} else {
return dateTimeFormatter.format(date);
}
}
答案 0 :(得分:2)
我有四种方法可以解决这个问题:
去寻找4。
使用com.google.gwt.i18n.client.DateTimeFormat中的方法进行日期/时间格式设置。
答案 1 :(得分:0)
gwttime是Joda-Time to GWT的一个端口,对于处理时间段和持续时间非常有用。
在您的情况下,您可以构建DateTime
或LocalDateTime
s,然后使用它们构建Period
以查找秒数,分钟数,小时数,天数等等。
例如:
private static int daysBetween(LocalDateTime start, LocalDateTime end) {
return new Period(start, end, PeriodType.days()).getDays();
}
这会找到start
和end
之间的完整天数。
你可以重复几秒钟,几分钟等,然后用结果模糊逻辑来得到你想要的输出字符串。