1. You need to set a date formate.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
2. Get Current Date.
Calendar c = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); String formattedDate = df.format(c.getTime()); System.out.println("====currentDateTimeString=" + formattedDate);
3. Prepare to compare with twodate.
try {
Date date1 = simpleDateFormat.parse("8-11-2016 10:30:10"); Date today = simpleDateFormat.parse("8-11-2016 10:30:10"); Date date_yeasterday = simpleDateFormat.parse("7-11-2016 10:30:10"); Date date_between_seven = simpleDateFormat.parse("5-11-2016 10:30:10"); Date date_after_seven = simpleDateFormat.parse("31-10-2016 10:30:10"); printDifference(date1, today, "8-11-2016 10:30:10"); printDifference(date1, date_yeasterday, "7-11-2016 10:30:10"); printDifference(date1, date_between_seven, "5-11-2016 10:30:10"); printDifference(date1, date_after_seven, "31-10-2016 10:30:10"); } catch (ParseException e) { e.printStackTrace(); }
4. Call Method only and you will get results like that:
Today: 10:12 pm
1 day ago: Yeasterday
Week Day: mon,tue,sat,sun
Before 7 day : date formate what event you want.
public void printDifference(Date startDate, Date endDate, String finalDate) { //milliseconds long different = endDate.getTime() - startDate.getTime(); System.out.println("startDate : " + startDate); System.out.println("endDate : " + endDate); System.out.println("different : " + different); long secondsInMilli = 1000; long minutesInMilli = secondsInMilli * 60; long hoursInMilli = minutesInMilli * 60; long daysInMilli = hoursInMilli * 24; long elapsedDays = different / daysInMilli; different = different % daysInMilli; long elapsedHours = different / hoursInMilli; different = different % hoursInMilli; long elapsedMinutes = different / minutesInMilli; different = different % minutesInMilli; long elapsedSeconds = different / secondsInMilli; System.out.printf( "%d days, %d hours, %d minutes, %d seconds%n", elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds); if (elapsedDays == 0) { System.out.printf( "%d days", elapsedDays); System.out.println("elapsedDays if same date" + formatTimeOnly(MainActivity.this, finalDate)); } else if (elapsedDays == (-1)) { System.out.printf( "%d days", elapsedDays); System.out.println("elapsedDays if before day Yesterday" + elapsedDays); } else if (elapsedDays == (-2) || elapsedDays == (-3) || elapsedDays == (-4) || elapsedDays == (-5) || elapsedDays == (-6) || elapsedDays == (-7)) { System.out.printf( "%d days", elapsedDays); SimpleDateFormat sdf = new SimpleDateFormat("EEE"); // Date d = new Date(); String dayOfTheWeek = sdf.format(endDate); System.out.println("elapsedDays if week day=====" + dayOfTheWeek); } else { System.out.printf( "%d days", elapsedDays); System.out.println("elapsedDays if greater then week day" + formatDateOnly(MainActivity.this, finalDate)); } } public static String formatTimeOnly(Context context, String timeToFormat) { String finalDateTime = ""; SimpleDateFormat iso8601Format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); Date date = null; if (timeToFormat != null) { try { date = iso8601Format.parse(timeToFormat); } catch (ParseException e) { date = null; } if (date != null) { long when = date.getTime(); int flags = 0; flags |= android.text.format.DateUtils.FORMAT_SHOW_TIME; finalDateTime = android.text.format.DateUtils.formatDateTime(context, when + TimeZone.getDefault().getOffset(when), flags); } } return finalDateTime; } public static String formatDateOnly(Context context, String timeToFormat) { String finalDateTime = ""; SimpleDateFormat iso8601Format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); Date date = null; if (timeToFormat != null) { try { date = iso8601Format.parse(timeToFormat); } catch (ParseException e) { date = null; } if (date != null) { long when = date.getTime(); int flags = 0; flags |= android.text.format.DateUtils.FORMAT_SHOW_DATE; flags |= android.text.format.DateUtils.FORMAT_SHOW_YEAR; finalDateTime = android.text.format.DateUtils.formatDateTime(context, when + TimeZone.getDefault().getOffset(when), flags); } } return finalDateTime; }