引用
import java.sql.Timestamp; import java.text.ParseException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class BaseModel { /** * @Des for view mode to show friendly record date. * @param modTs(record data time) * @return Today, Yesterday, More than 1 week ... */ public String getFriendStr(Timestamp modTs) { Date todayDate = new Date(); Date recordDate = new Date(); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(); df.applyPattern("yyyy-MM-dd"); try { recordDate = df.parse(modTs.toString()); todayDate = df.parse(todayDate.toString()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } String friendlyStr = ""; int countDays = this.daysBetween(recordDate,todayDate); int countMonths = this.getDateDif(recordDate,todayDate)[1]; if(countDays>100 || countMonths>2){ friendlyStr = "More than 3 months"; }else if(countMonths == 1 || countMonths == 2){ friendlyStr = "More than 1 month"; }else if(countDays >= 14){ friendlyStr = "More than 2 weeks"; }else if(countDays >= 7){ friendlyStr = "More than 1 week"; }else if(countDays >= 4 && countDays = 3){ friendlyStr = "3 days before"; }else if(countDays >= 1){ friendlyStr = "Yesterday"; }else { friendlyStr = "Today"; /*df.applyPattern("HH:mm:ss"); friendlyStr = df.format(modTs);*/ } return friendlyStr; } /** * @Des: Get Difference Between Two Dates in Days, Months and Year format. * @param date1 * @param date2 * @return */ public int[] getDateDif(Date date1, Date date2) { int[] monthDay = { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; Calendar fromDate; Calendar toDate; int increment = 0; int[] ageDiffArr = new int[3]; int year; int month; int day; Calendar d1 = new GregorianCalendar().getInstance(); d1.setTime(date1); Calendar d2 = new GregorianCalendar().getInstance(); d2.setTime(date2); if (d1.getTime().getTime() > d2.getTime().getTime()) { fromDate = d2; toDate = d1; } else { fromDate = d1; toDate = d2; } if (fromDate.get(Calendar.DAY_OF_MONTH) > toDate.get(Calendar.DAY_OF_MONTH)) { increment = monthDay[fromDate.get(Calendar.MONTH)]; } GregorianCalendar cal = new GregorianCalendar(); boolean isLeapYear = cal.isLeapYear(fromDate.get(Calendar.YEAR)); if (increment == -1) { if (isLeapYear) { increment = 29; } else { increment = 28; } } // DAY CALCULATION if (increment != 0) { day = (toDate.get(Calendar.DAY_OF_MONTH) + increment) - fromDate.get(Calendar.DAY_OF_MONTH); increment = 1; } else { day = toDate.get(Calendar.DAY_OF_MONTH) - fromDate.get(Calendar.DAY_OF_MONTH); } // MONTH CALCULATION if ((fromDate.get(Calendar.MONTH) + increment) > toDate.get(Calendar.MONTH)) { month = (toDate.get(Calendar.MONTH) + 12) - (fromDate.get(Calendar.MONTH) + increment); increment = 1; } else { month = (toDate.get(Calendar.MONTH)) - (fromDate.get(Calendar.MONTH) + increment); increment = 0; } // YEAR CALCULATION year = toDate.get(Calendar.YEAR) - (fromDate.get(Calendar.YEAR) + increment); ageDiffArr[0] = day; ageDiffArr[1] = month; ageDiffArr[2] = year; return ageDiffArr; // RESULT AS DAY, MONTH AND YEAR in form of Array } /** * @Des Get Difference Between Two Dates in Days (Total days) * @param early * @param late * @return */ public int daysBetween(Date early, Date late) { java.util.Calendar calst = java.util.Calendar.getInstance(); java.util.Calendar caled = java.util.Calendar.getInstance(); calst.setTime(early); caled.setTime(late); // init set time to zero. calst.set(java.util.Calendar.HOUR_OF_DAY, 0); calst.set(java.util.Calendar.MINUTE, 0); calst.set(java.util.Calendar.SECOND, 0); caled.set(java.util.Calendar.HOUR_OF_DAY, 0); caled.set(java.util.Calendar.MINUTE, 0); caled.set(java.util.Calendar.SECOND, 0); // get between tow one. int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24; return days; } }