public static Date stringToDate(String dateString) { try { MDebug.log("DATE: [|" + dateString + "|]"); return formatter.parse(dateString); } catch (Exception ex) { ex.printStackTrace(); return new Date(); } }
public static String getShowString(String current, String then) { try { Date c = formatter.parse(current); Date t = formatter.parse(then); MDebug.log("|" + (c.getTime() - t.getTime()) + "|"); } catch (Exception ex) { } return "right"; }
public static String getTimeShow(String dateString) { Date thenDate = stringToDate(dateString); Calendar then = Calendar.getInstance(); then.setTime(thenDate); Calendar current = Calendar.getInstance(); long differ = current.getTimeInMillis() - then.getTimeInMillis(); if (differ < 0) { return "Milai"; } else { if (differ >= 0L && differ < 300000L) { return "Just now"; } if (differ >= 300000L && differ < 10800000L) { return MessageFormat.format("{0}:{1}", then.get(Calendar.HOUR), then.get(Calendar.MINUTE)); } if (then.get(Calendar.YEAR) != current.get(Calendar.YEAR)) { return MessageFormat.format( "{0}/{1}/{2}", then.get(Calendar.YEAR) - 2000, then.get(Calendar.MONTH), then.get(Calendar.DATE)); } if (current.get(Calendar.DAY_OF_YEAR) - then.get(Calendar.DAY_OF_YEAR) == 1) { return "Yesterday"; } if (then.get(Calendar.MONTH) != current.get(Calendar.MONTH)) { MDebug.log("[DATE]" + then.get(Calendar.DATE)); return MessageFormat.format( "{0}/{1}/{2}", then.get(Calendar.YEAR) - 2000, then.get(Calendar.MONTH), then.get(Calendar.DAY_OF_MONTH)); } int dayDiff = current.get(Calendar.DAY_OF_MONTH) - then.get(Calendar.DAY_OF_MONTH); if (dayDiff > 7) { return MessageFormat.format( "{0}/{1}/{2}", then.get(Calendar.YEAR) - 2000, then.get(Calendar.MONTH), then.get(Calendar.DATE)); } else { if (dayDiff == 0) { int hour = then.get(Calendar.HOUR); if (0 <= hour && hour < 5) { return "MidNight"; } else if (5 <= hour && hour < 12) { return "BeforeNoon"; } else if (12 <= hour && hour < 19) { return "AfterNoon"; } else if (19 <= hour && hour != 0) { return "Night"; } } if (current.get(Calendar.WEEK_OF_MONTH) == then.get(Calendar.WEEK_OF_MONTH)) { return getDayString(then.get(Calendar.DAY_OF_WEEK)); } else { return MessageFormat.format( "{0}/{1}/{2}", then.get(Calendar.YEAR) - 2000, then.get(Calendar.MONTH), then.get(Calendar.DATE)); } } } }