我有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得到真正的月; ?
什麼是崩潰的堆棧跟蹤?我假設爲空指針 –
如果我這樣做:'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
致命異常:主要(非法參數異常 - 當前應該> =開始和<=結束)。這是以前的代碼。正如我在上面的評論中所述,我找到了一種將其設置爲不同日期的方法,但不是我的自定義日期...所以我沒有收到更多錯誤,我只是沒有我想要的日期 – user1137313