Difference in days between two dates in Java?

后端 未结 19 1941
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 08:26

I need to find the number of days between two dates: one is from a report and one is the current date. My snippet:

  int age=calculateDiffer         


        
19条回答
  •  自闭症患者
    2020-11-22 08:55

    This code calculates days between 2 date Strings:

        static final long MILLI_SECONDS_IN_A_DAY = 1000 * 60 * 60 * 24;
        static final String DATE_FORMAT = "dd-MM-yyyy";
        public long daysBetween(String fromDateStr, String toDateStr) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
        Date fromDate;
        Date toDate;
        fromDate = format.parse(fromDateStr);
        toDate = format.parse(toDateStr);
        return (toDate.getTime() - fromDate.getTime()) / MILLI_SECONDS_IN_A_DAY;
    }
    

提交回复
热议问题