计算开始日期和结束日期之间经过的天,月和年

时间:2018-08-30 13:50:34

标签: java

我正在使用Windows 10计算器等日期计算应用程序 您选择开始和结束日期的地方 并计算月份和年份的天数

  public String differenceInDates(){
                Date start_date = convertStringToDate("2017-04-24");
                Date end_date = convertStringToDate("2018-07-24");
                long different = this.end_date.getTime() - this.start_date.getTime();

                        long millisInSeconds = 1000;
                        long millisInMinutes = millisInSeconds * 60;
                        long millisInHours = millisInMinutes * 60;
                        long millisInDay = millisInHours *24;

                        long elapsedDays = different / millisInDay;
                        different = different % millisInDay;

                        long elapsedHours = different / millisInHours;
                        different = different % millisInHours;

                        long elapsedMinutes = different / millisInMinutes;
                        different = different % millisInMinutes;

                        long elapsedSeconds = different / millisInSeconds;
                        if (elapsedHours == 0){
                            return String.valueOf("days"+elapsedDays+":"+":"+elapsedMinutes+":"+elapsedSeconds);
                        }
                        if (elapsedMinutes == 0){
                            return String.valueOf("days"+elapsedDays+":"+":"+elapsedHours+":"+elapsedSeconds);
                        }
                        if (elapsedHours == 0 && elapsedMinutes == 0){
                            return String.valueOf("days"+elapsedDays);
                        }
                        return String.valueOf("days"+elapsedDays+":"+" Hours"+elapsedHours+":"+" Minutes"+elapsedMinutes+":"+elapsedSeconds);
                }

                private Date convertStringToDate(String strDate) throws ParseException {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = simpleDateFormat.parse(strDate);
                        return date;
                    }

我想要这样的答案

  

1年3个月

2 个答案:

答案 0 :(得分:2)

java.time

如果您可以使用LocalDatePeriod,那么这很简单:

LocalDate date1 = LocalDateTime.of(2018, Month.SEPTEMBER, 2, 13, 40);
LocalDate date2 = LocalDateTime.of(2018, Month.SEPTEMBER, 2, 13, 40);
Period period = Period.between(date1, date2);
System.out.println(period.getYears() + " year " + period.getMonths() + " months");

答案 1 :(得分:0)

您可以使用以下代码:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
            String CURRDATE = "16-07-1991";
            String EFFDATE = "28-08-2018";

            Date startdate = null;
            Date enddate = null;
            try {
                startdate = formatter.parse(CURRDATE);
                enddate = formatter.parse(EFFDATE);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            Calendar startCalendar = new GregorianCalendar();
            startCalendar.setTime(startdate);

            Calendar endCalendar = new GregorianCalendar();
            endCalendar.setTime(enddate);

            int monthCount = 0;
            int firstDayInFirstMonth = startCalendar.get(Calendar.DAY_OF_MONTH);
            startCalendar.set(Calendar.DAY_OF_MONTH, 1);
            endCalendar.add(Calendar.DAY_OF_YEAR, -firstDayInFirstMonth + 1);

            while (!startCalendar.after(endCalendar)) {
                startCalendar.add(Calendar.MONTH, 1);
                ++monthCount;
            }

            startCalendar.add(Calendar.MONTH, -1);
            --monthCount;
            int remainingDays = 0;
            while (!startCalendar.after(endCalendar)) {
                startCalendar.add(Calendar.DAY_OF_YEAR, 1);
                ++remainingDays;
            }

            startCalendar.add(Calendar.DAY_OF_YEAR, -1);
            --remainingDays;

            int lastMonthMaxDays = endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            if (remainingDays >= lastMonthMaxDays) {
                ++monthCount;
                remainingDays -= lastMonthMaxDays;
            }

            int diffMonth = monthCount % 12;
            int diffYear = monthCount / 12;
            int diffDay = remainingDays;

            System.out.println(diffYear + " Year(s) and " + diffMonth + " Month(s) and " + diffDay + " Day(s)");

结果是:

27 Year(s) and 1 Month(s) and 12 Day(s)