不同日期之间返回相同的天数

时间:2018-11-14 05:27:00

标签: android

我在新的Java项目中使用日期转换器,在这里我计算两天之间的差额以便在两天之间进行转换。一切正常,但我陷入了一个问题:

private long daysBetween(Calendar startDate, Calendar endDate) {
    Calendar date = (Calendar) startDate.clone();
    long daysBetween = 0;
    while (date.before(endDate)) {
        date.add(Calendar.DAY_OF_MONTH, 1);
        daysBetween++;
    }
    Log.e("Days Between check",daysBetween + "This is the days difference");
    return daysBetween;
}

这是我的函数,用于计算两个日期之间的天数。这些日期是从微调器中选择的,我记录了这些值以进行检查。它提供了真正的输出,直到2018/03/31和2018/4/1从该函数产生相同的天数为止。

这是我的其他功能(如果需要):

convertToNepali.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Calendar currentEngDate = new GregorianCalendar();
        int engYear = startingEngYearForSelection;
        int engMonth = startingEngMonthForSelection;
        int engDay = startingEngDayForSelection;

        if(yearEngSpinner.getSelectedItem()!=null)
        {
            engYear = Integer.parseInt(yearEngSpinner.getSelectedItem().toString());
        }

        if(monthEngSpinner.getSelectedItem()!=null)
        {
            Date date = null;
            Log.e("monthSpinner","I am in month spinner");
            try {
                date = new SimpleDateFormat("MMMM").parse(monthEngSpinner.getSelectedItem().toString());
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);

                engMonth = Integer.parseInt(String.valueOf(cal.get(Calendar.MONTH)+1));

            } catch (ParseException e) {
                e.printStackTrace();
                engMonth = 1;
                Log.e("Error Some Error", e.getMessage());
            }

        }

        if(dayEngSpinner.getSelectedItem()!=null)
        {
            engDay = Integer.parseInt(dayEngSpinner.getSelectedItem().toString());
        }

        currentEngDate.set(engYear, engMonth, engDay);

        Calendar baseEngDate = new GregorianCalendar();

        baseEngDate.set(startingEngYearForSelection, startingEngMonthForSelection, startingEngDayForSelection);

        long totalEngDaysCount = daysBetween(baseEngDate, currentEngDate);

        String textOfNepaliVersion = convertToNepaliFunction(totalEngDaysCount);

        dateConvertedTextView.setText(textOfNepaliVersion);

    }



});

0 个答案:

没有答案