经过的月数计算未给出当前月和日但不同年份的日期的正确结果 [英] Elapsed Months calculation not giving correct result for dates with current month and day but a different year

查看:147
本文介绍了经过的月数计算未给出当前月和日但不同年份的日期的正确结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算自特定日期以来经过的月份,该功能正常工作,虽然它给了我一个月的差异(不到一个月),如果我把今天的日期与过去的不同年份。

I am trying to calculate months elapsed since a particular date, the function works fine albeit it gives me a one months difference (Less than one month) if I put a date of today with a different year of the past.

假设该功能运作良好的所有日期,除了:

Say for all dates the function works well, except:

如果是2014-03-06 (YYYY,MM,DD)今天我将该日期作为2006-03-06处理到该函数,结果缩短了一个月,但如果我明天使用相同的日期,结果就好了。在哪里我错了。下面是代码:

If it is "2014-03-06" (YYYY,MM,DD) today and I process a date as "2006-03-06" to the function, the result is one month shorter, however if I use the same date tomorrow, the result is just fine. WOnder where I am going wrong. Below is the code:

public static int months(String d) {

        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = null;
        try {
            d1 = f.parse(d);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //   Date d2 = (getCurrentDate());
        Date d2 = getCurrentDate(); 
        Calendar c1 = Calendar.getInstance();
        c1.setTime(d1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(d2);
        int diff = 0;
        if (c2.after(c1)) {
            while (c2.after(c1)) {
                c1.add(Calendar.MONTH, 1);
                if (c2.after(c1)) {
                    diff++;
                }
            }
        } else if (c2.before(c1)) {
            while (c2.before(c1)) {
                c1.add(Calendar.MONTH, -1);
                if (c1.before(c2)) {
                    diff--;
                }
            }
        }
        System.out.println(diff);
        return diff;
    }



结果:95个月。



理想情况下它应该是96个月。



请不要考虑函数的静态性质,我已将此代码隔离在另一个项目中以用于测试目的。

Result: 95 months.

Ideally it should be 96 months.

Please dont consider the static nature of functions, I have isolated this code in a different project for test purpose.

这是c1和c2的值:

java.util.GregorianCalendar[time=1141583400000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2006,MONTH=2,WEEK_OF_YEAR=10,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,DAY_OF_YEAR=65,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0] and c2 is: java.util.GregorianCalendar[time=1394044200000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=2,WEEK_OF_YEAR=10,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,DAY_OF_YEAR=65,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0]
95

我的错误是getCurrentDate():

My bad here is the getCurrentDate():

public static Date getCurrentDate() {
        String DateStr = new SimpleDateFormat("yyyy-MM-dd"/*,
                current*/).format(Calendar.getInstance().getTime());
        //  String DateStr = formattedDate;
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd"/*, current*/).parse(DateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date dx = new java.sql.Date(date.getTime());
        return dx;
    }


推荐答案

检查 getCurrentDate()方法。我用替换了新的Date(),输出为 96 ,如你所说。

Check your getCurrentDate() method. I have replaced with new Date() and output is 96 as you said.

这篇关于经过的月数计算未给出当前月和日但不同年份的日期的正确结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆