项目中时间处理----今天:时分(10:15),昨天/前天:(昨天/前天),除此之外的本周(星期几),再往前年.月.日(2017.06.15)
导入:
import org.joda.time.LocalDateTime;
//格式化时间 private String formatTime(LocalDateTime now, Date beginDate) { LocalDateTime beginDt = new LocalDateTime(beginDate.getTime()); if (now.getYear() == beginDt.getYear() && now.getMonthOfYear() == beginDt.getMonthOfYear() && now.getDayOfMonth() == beginDt.getDayOfMonth()) { return beginDt.toString("HH:mm"); } if (now.getYear() == beginDt.getYear() && now.getMonthOfYear() == beginDt.getMonthOfYear() && now.getDayOfMonth() == beginDt.getDayOfMonth() + 1) { return "昨天"; } if (now.getYear() == beginDt.getYear() && now.getMonthOfYear() == beginDt.getMonthOfYear() && now.getDayOfMonth() == beginDt.getDayOfMonth() + 2) { return "前天"; } if (now.getYear() == beginDt.getYear() && now.getMonthOfYear() == beginDt.getMonthOfYear() && now.getWeekOfWeekyear() == beginDt.getWeekOfWeekyear()) { int i = beginDt.getDayOfWeek(); if (i == 1) { return "星期一"; } else if (i == 2) { return "星期二"; } else if (i == 3) { return "星期三"; } else if (i == 4) { return "星期四"; } else if (i == 5) { return "星期五"; } else if (i == 6) { return "星期六"; } else { return "星期天"; } } return beginDt.toString("yyyy.MM.dd"); }
调用的时候这样调用:
LocalDateTime now = LocalDateTime.now(); for (ShortMessage s : messageList) { Date beginDate = s.getBeginDate(); s.setStartDate(formatTime(now, beginDate)); }