-4

I have two dates with string format "16-Feb-2017", "26-Feb-2017" and I used

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");

but I am unable to get exact result like "10".

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
NareshY
  • 1
  • 1

4 Answers4

2

Hope this will help you.pass your dates in myDate and time_ago.

int totalMin;

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH);

Date systemDate = Calendar.getInstance().getTime();
                        String myDate = sdf.format(systemDate);


Date Date1 = null;
try {
    Date1 = sdf.parse(myDate);

} catch (ParseException e) {
    e.printStackTrace();
}
Date Date2 = null;
try {
    Date2 = sdf.parse(time_ago);

} catch (ParseException e) {
    e.printStackTrace();
}

assert Date2 != null;
assert Date1 != null;
long millse = Date1.getTime() - Date2.getTime();


long mills = Math.abs(millse);

Hours = (int) (mills / (1000 * 60 * 60));
Mins = (int) (mills / (1000 * 60)) % 60;
Secs = (int) (mills / 1000) % 60;

long diffDays = millse / (24 * 60 * 60 * 1000);

if (Secs >= 60) {
    Mins = Mins + 1;
    Secs = Secs - 60;
} else if (Mins >= 60) {
    Hours = Hours + 1;
    Mins = Mins - 60;
}

totalMin = (int) ((Mins) + (Secs / 60));
String t_time;

if (diffDays > 0) {
    if (diffDays == 1) {
        t_time = diffDays + " day";
    } else {
        t_time = diffDays + " days";
    }

} else if (Hours > 0) {
    if (Hours == 1) {
        t_time = Hours + " hour";
    } else {
        t_time = Hours + " hours";
    }

} else if (Mins > 0) {
    if (Mins == 1) {
        t_time = totalMin + " minute";
    } else {
        t_time = totalMin + " minutes";
    }

} else {
    if (Secs == 1) {
        t_time = Secs + " second";
    } else {
        t_time = Secs + " seconds";
    }

}
aUserHimself
  • 1,589
  • 2
  • 17
  • 26
appy
  • 106
  • 1
  • 5
  • When input is like `16-Feb-2017` without any hour or minute, I wouldn’t expect hours, minutes and seconds to be needed in the output. The original asker can best tell. – Ole V.V. Feb 16 '17 at 12:25
1
DateTimeFormatter formatter =  DateTimeFormatter.ofPattern("dd-MMM-yyyy");
java.time.LocalDate d1 = java.time.LocalDate.parse("16-Feb-2017", formatter);
java.time.LocalDate d2 = java.time.LocalDate.parse("26-Feb-2017", formatter);

Period until = d1.until(d2);
System.out.println("Dif: " + until.getDays());
kamehl23
  • 522
  • 3
  • 6
  • Thanks for your quick response ... This is not applicable in android, can u please help me suitable with android – NareshY Feb 16 '17 at 08:29
  • This is the recommended way for anyone who can use Java 8 (or later). I am upvoting to help guide any readers who can. – Ole V.V. Feb 16 '17 at 08:38
  • Beware the `DateTimeFormatter.ofPattern("dd-MMM-yyyy")` uses the default format locale for the computer. If you need some other locale, you may add a `Locale` argument. – Ole V.V. Feb 16 '17 at 09:00
  • You are right, Ole V.V. concerning the Locale. – kamehl23 Feb 16 '17 at 09:02
0

Please use below code to init SimpleDateFormat.

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);

Tom Grylls
  • 121
  • 7
  • Thanks for your quick response ... I need a difference between 2 Dates...Please let me know how can I proceed – NareshY Feb 16 '17 at 08:19
0

As I already said in a comment, use Java 8 java.time classes if you can. kamehl23’s answer shows you how. It’s a both elegant and robust solution, also across changes to and from summer time (DST).

EDIT: Stuck with an older Java version, they say you can use either ThreeTenABP or Joda time, I haven’t tried any of them. ThreeTenABP, I read, is an Android adaption of a backport of java.time to Java 6 and 7, so I would be tempted to give that a shot.

You can of course get through with Java 1.1 Calendar. The solution that also works across summer time change is:

    String formattedDate1 = "16-Feb-2017";
    String formattedDate2 = "26-Feb-2017";

    DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", YOUR_LOCALE);

    Date d1 = df.parse(formattedDate1);
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(d1);

    Date d2 = df.parse(formattedDate2);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(d2);

    int daysBetween = 0;
    while (cal1.before(cal2)) {
        daysBetween++;
        cal1.add(Calendar.DATE, 1);
    }
    System.out.println(daysBetween);

This prints 10. It’s neither very elegant nor very efficient, but it works robustly as long as the ‘from’ date is before (or the same as) the ‘to’ date (which can easily be checked).

Community
  • 1
  • 1
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161