2016-12-27 5 views
-1

に取り組んでいないカレンダーに日追加:私はここで週の日の配列とそれらの日付を作成してい私は次のコードを持っているのAndroid

public void createWeeks(JSONArray TargetDays){ 
    int helper, targetDayPos, curDayPos; 
    JSONArray dateContainer = new JSONArray(); 
    JSONArray dateData = new JSONArray(); 
    Calendar c = Calendar.getInstance(); 
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); 
    Utilities dayConverter = new Utilities(getApplicationContext()); 

    for(int i=0 ; i<TargetDays.length() ; i++){ 
     try { 
      targetDayPos = dayConverter.getDayPosition(TargetDays.getString(i)); 
      curDayPos = dayConverter.getCalenderDayPosition(dayOfWeek); 
      if (curDayPos > targetDayPos) { 
       helper = curDayPos - targetDayPos; 
       c.add(Calendar.DATE, -helper); 
      } 
      else if(targetDayPos > curDayPos){ 
       helper = targetDayPos - curDayPos; 
       c.add(Calendar.DATE, helper); 
      } 

      dateData.put(c.get(Calendar.YEAR)); 
      dateData.put(c.get(Calendar.MONTH)+1); 
      dateData.put(c.get(Calendar.DAY_OF_MONTH)+1); 
      Toast.makeText(getApplicationContext(),curDayPos+"\n"+targetDayPos,Toast.LENGTH_LONG).show(); 
      dateContainer.put(dateData); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    try { 
     weekDayList.put("days",TargetDays); 
     weekDayList.put("dates", dateContainer); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

を。日を追加することはできませんし、最後には今日の日付を取得しています。

+0

は、あなたが([、最小完全、かつ検証例]を作るでしhttp://stackoverflow.com/help/マック)私はそれが大いに役立つと確信しています(私たちとあなた)。 –

+0

私はカレンダーの "追加"方法について助けが必要です。それは何もしていない。何も起こりません。私はそれと一緒に日を追加した後、私はそれがまだ追加する前に同じ日付だ時間を得る。 @Ole V.V. –

+0

私は 'Calendar.add()'にかなりの自信を持っています。私はあなたの問題がどこか別のものだと思っています。デバッガを試しましたか? –

答えて

0

この問題を発見したのは@Ole V.V.コメントではカレンダーに関するものではないと述べた。 問題はそのアルゴリズムに関するものでした。それは私がjson配列を作成していた方法です。

私はこれにそれを変更し、私のプログラムでは異なる方法でそれを扱っ:

public void createWeeks(JSONArray TargetDays){ 
    int helper, targetDayPos, curDayPos; 
    JSONArray dateContainer = new JSONArray(); 
    Date currentDate = new Date(); 
    Calendar c = Calendar.getInstance(); 
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); 
    Utilities dayConverter = new Utilities(getApplicationContext()); 

    try { 
     for(int i=0 ; i<TargetDays.length() ; i++){ 

      c.setTime(currentDate); 
      targetDayPos = dayConverter.getDayPosition(TargetDays.getString(i)); 
      curDayPos = dayConverter.getCalenderDayPosition(dayOfWeek); 
      if (curDayPos > targetDayPos) { 
       helper = curDayPos - targetDayPos; 
       c.add(Calendar.DATE, -helper); 
      } 
      else if(targetDayPos > curDayPos) { 
       helper = targetDayPos - curDayPos; 
       c.add(Calendar.DATE, helper); 
      } 
      dateContainer.put(dateFormat.format(c)); 

     } 
     weekDayList.put("days",TargetDays); 
     weekDayList.put("dates", dateContainer); 


    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
関連する問題