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".
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".
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";
}
}
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());
Please use below code to init SimpleDateFormat.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
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).