Java Date get days between two String date values
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static void main(String[] argv) throws Exception { String date1 = "2020-02-03"; String date2 = "2010-02-03"; System.out.println(daysBetween(date1, date2)); }/*from w w w.ja v a 2s.c o m*/ public static int daysBetween(String date1, String date2) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); if (date1 == null || date1.trim().length() == 0 || date2 == null || date2.trim().length() == 0) { return 0; } Date smdate = sdf.parse(date1); Date bdate = sdf.parse(date2); Calendar cal = Calendar.getInstance(); cal.setTime(smdate); long time1 = cal.getTimeInMillis(); cal.setTime(bdate); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); } }