2013-08-03 117 views
2

我有3個包含日期,月份和年份值的字符串。例如:Android將DatePicker設置爲某個日期

String mday = "02"; 
String mmonth="07"; 
String myear="2013"; 

我需要將我的活動中的DatePicker設置爲從上述日期起的一個月。我並不是說只需將1加到月份價值...在第31天的情況下,我最終會得到一個無效的日期。 所以我需要一些方法來增加日期(有效的方式)並設置DatePicker與它的值。 我知道,設置與國際價值的日期選擇器是這樣完成的:

DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1); 
datepicker.init(iYear, iMonth, iDay, null); 
// where iYear,iMonth and iDay are integers 

但我怎麼個月獲得遞增的日期的日,月,年的整數值?

因此,在第一個值(字符串)和遞增日期(整數)的最終值之間,我必須完成哪些步驟?我想我將不得不使用日曆。 所以我的代碼應該是這樣的:

Integer iYear, iMonth, iDay = 0; 
String mday = "02"; 
String mmonth="07"; 
String myear="2013"; 

Calendar cal = Calendar.getInstance(); 
cal.set(Integer.parseInt(myear), Integer.parseInt(mmonth), Integer.parseInt(mday)); 
cal.add(Calendar.MONTH, 1); 
// here I should get the values from cal inside the iYear, iMonth, iDay, but I do not seem to succeed. 

DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1); 
datepicker.init(iYear, iMonth, iDay, null); 

如果我這樣做:

datepicker.init(cal.YEAR, cal.MONTH, cal.DATE, null); 

然後應用程序崩潰。 我該怎麼辦? 如何設置這個增加一個月的日期到我的DatePicker?

UPDATE 我改變了我的測試代碼如下:

Calendar cal = Calendar.getInstance(); 
    cal.set(2013, 05, 23); 
    cal.add(Calendar.MONTH, 1); 

    int xxday = cal.get(Calendar.DATE); 
    int xxmonth = cal.get(Calendar.MONTH); 
    int xxyear = cal.get(Calendar.YEAR); 

    datepicker.init(xxyear, xxmonth, xxday, null); 

但現在的日期選擇器設置爲一個月從現在開始,而不是一個一個月想要的日期,以便代替(2013-06- 23)我有(2013-09-23)。我認爲這是因爲

int xxmonth = cal.get(Calendar.MONTH); 

我怎樣才能從一個日曆cal得到真正的月; ?

+0

什麼是崩潰的堆棧跟蹤?我假設爲空指針 –

+0

如果我這樣做:'Calendar cal = Calendar.getInstance(); cal.set(2013,07,23); cal.add(Calendar.MONTH,1); int xxday = cal.get(Calendar.DATE); int xxmonth = cal.get(Calendar。月); int xxyear = cal.get(Calendar.YEAR); datepicker.init(xxyear,xxmonth,xxday,null);'然後我的datepicker設置爲2019年9月23日,而不是23.08.2013。我認爲這是因爲我訪問了今天設置的Calendar.MONTH?!?!? – user1137313

+0

致命異常:主要(非法參數異常 - 當前應該> =開始和<=結束)。這是以前的代碼。正如我在上面的評論中所述,我找到了一種將其設置爲不同日期的方法,但不是我的自定義日期...所以我沒有收到更多錯誤,我只是沒有我想要的日期 – user1137313

回答

2

日曆月份爲0。所以07月是8月。

+0

所以我基本上可以這樣做:'cal.add(Calendar.MONTH-1,1);'?它安全嗎? – user1137313

+0

我看到,如果我做了上面的代碼,它的工作原理......但如果我的初始日期是2013年1月31日,那麼DatePicker中的最後日期是Mar.03.2013。所以我猜想在一個日曆日期中添加一個月是爲它增加了31天?這是正常的嗎? – user1137313

1

使用下面的代碼,如果你有一個日期選取器初始化日曆對象:

Calendar calendar = new GregorianCalendar(datePicker.getYear(), 
           datePicker.getMonth(), 
           datePicker.getDayOfMonth()); 

否則硬編碼

5

DatePicker類有一個方法updateDate(year, month, dayOfMonth)構造函數的日期部分,你可以使用設定的日期在你DatePicker,如下圖所示:

DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1); 
datePicker.updateDate(2016, 5, 22); 
+0

kamran ..謝謝:) – usetej2

0

使用該政黨成員創建您DatePickerDi考勤然後用這個代碼裏面DatePickerDialog https://developer.android.com/guide/topics/ui/dialogs.html

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    String mday = "02"; 
    String mmonth="07"; 
    String myear="2013"; 
    //convert them to int 
    int mDay=Integer.valueOf(mday); 
    int mMonth=Integer.valueOf(mmonth); 
    int mYear=Integer.valueOf(myear); 

    return new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() { 
     @Override 
     public void onDateSet(DatePicker datePicker, int i, int i1, int i2) { 
      String d=convertToCompletDate(i2,i1,i); 
      mListener.onDatePicked(d); 

     } 
    },mYear,mMonth,mDay); 
} 
相關問題