首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这个目标日期计算器出了什么问题?

这个目标日期计算器出了什么问题?
EN

Stack Overflow用户
提问于 2011-05-08 05:23:28
回答 1查看 113关注 0票数 0

我前段时间为我的项目写了这段代码,它应该计算用户可能实现其饮食目标的目标日期。但是在测试应用程序时,我发现无论目标是什么,目标日期总是比创建的日期晚一个月。

这是代码

代码语言:javascript
复制
    public String calcTD(SQLiteDatabase db){
    String date = null;
    myDataBase = db;
    Cursor c = myDataBase.rawQuery("SELECT * FROM USER", null);
    if (c!= null){
        if (c.moveToFirst()){
          String cdate = c.getString(c.getColumnIndex("CreatedDate"));              
          String type = c.getString(c.getColumnIndex("GoalType"));              
          if (type == "Maintain Weight")
                date = cdate;
          else{               
            int rate = c.getInt(c.getColumnIndex("Rate"));
            float gw = c.getFloat(c.getColumnIndex("GoalWeight"));
            float cw = getCW(db); //get current weight
            float w = 0; int days = 0;
            if (type == "Lose Weight")
                 w = cw - gw;
            else if (type == "Gain Weight")
                w = gw - cw;                
            switch (rate){
            case 0: // the rate 250g/week
                days = Math.round(w * 7 * 4); 
                break;
            case 1: //the rate 500g/week
                days = Math.round(w * 7 * 2); 
                break;
            case 2://the rate 750g/week
                days = Math.round(w * 7 * (4/3)) ;
                break;
            case 3:the rate 1kg/week
                days = Math.round(w * 7);
                break;
            }
            int y, m, d;
                            // the currentDate format (YYYYMMDD)
            y = Integer.parseInt(cdate.substring(0,4));
            m = Integer.parseInt(cdate.substring(4,6));
            d = Integer.parseInt(cdate.substring(6,8));             
            GregorianCalendar  i = new GregorianCalendar(y, m, d);
            i.add(Calendar.DAY_OF_MONTH, days);
            date = AllCmnMtd.getDate(i);    // this will convert the date to a string with format (yyyymmdd)            

            }               
        }
    }
    return date;    
}

很抱歉我是这么讨厌的人,但是我的项目答辩在两周内,我在测试应用程序的时候遇到了很多错误!!:(.

谢谢并致以最良好的问候

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-08 05:36:38

Java的Calendar类在月份中使用基于0的索引。例如。0是一月,11是十二月。

在放入Calendar对象之前,从m中减去1。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5924082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档