我设置calendar.DAY_OF_MONTH时遇到问题

时间:2017-05-10 06:22:20

标签: android android-calendar

我想设置一个时间在两个时间范围之间,所以我得到当前时间,设置第一次和第二次比较。

但我发现DAY_OF_MONTH的值很奇怪。为什么它显示错误的价值? 有人可以教我如何解决它吗?

    // load current time
    Calendar current = Calendar.getInstance();

    //set first time
    Calendar firstLimit= Calendar.getInstance();
    firstLimit.set(Calendar.MONTH,4);
    firstLimit.set(Calendar.DAY_OF_MONTH,8);
    firstLimit.set(Calendar.HOUR, 12);
    firstLimit.set(Calendar.MINUTE, 30);
    firstLimit.set(Calendar.SECOND, 30);

    //set second time
    Calendar secondLimit= Calendar.getInstance();
    firstLimit.set(Calendar.MONTH,4);
    firstLimit.set(Calendar.DAY_OF_MONTH,12);
    secondLimit.set(Calendar.HOUR, 12);
    secondLimit.set(Calendar.MINUTE, 30);
    secondLimit.set(Calendar.SECOND, 30);

    //Wed May 10 06:14:04 GMT+00:00 2017
    Log.d("MONTH: current>",current.getTime()+"");
    //Fri May 12 12:30:30 GMT+00:00 2017  --> i thought it should be May 8
    Log.d("MONTH: firstLimit>",firstLimit.getTime()+"");
    //Wed May 10 12:30:30 GMT+00:00 2017 --> i thought it should be May 12
    Log.d("MONTH: secondLimit>",secondLimit.getTime()+"");

1 个答案:

答案 0 :(得分:1)

您在行后设置firstLimit - > //设置第二次

将firstLimit替换为secondLImit

//set second time
Calendar secondLimit= Calendar.getInstance();
secondLimit.set(Calendar.MONTH,4);   //replaced firstLimit with secondLImit
secondLimit.set(Calendar.DAY_OF_MONTH,12); //replaced firstLimit with secondLImit

secondLimit.set(Calendar.HOUR, 12);
secondLimit.set(Calendar.MINUTE, 30);
secondLimit.set(Calendar.SECOND, 30);