2014-12-02 166 views
-1

我寫了一個程序,根據從日期選擇器選擇日期來計算人的年齡,其工作的罰款。 現在我增加了一個方法來顯示只剩余天數,並在其上的人的生日是他的下一個生日當天如何計算Android中日期選擇器選擇日期後下一個生日的剩餘日期?

private void calculateNextBirthday() { 
    // TODO Auto-generated method stub 

    Date dt = new Date(); 
    SimpleDateFormat sdf = new SimpleDateFormat(
       "yyyy/MM/dd"); 
    final String strBDay = sdf.format(dt); 
    try { 

     dt = sdf.parse(strBDay); 
    } catch (final java.text.ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    final Calendar c = Calendar.getInstance(); 

    c.setTime(dt); 
    // c.add(Calendar.DATE, value); 
    final Calendar today = Calendar.getInstance(); 
    // Take your DOB Month and compare it to current 
    // month 
    final int BMonth = c.get(Calendar.MONTH); 
    final int CMonth = today.get(Calendar.MONTH); 
    c.set(Calendar.YEAR, today.get(Calendar.YEAR)); 
    c.set(Calendar.DAY_OF_WEEK, 
       today.get(Calendar.DAY_OF_WEEK)); 
    if (BMonth <= CMonth) { 
     c.set(Calendar.YEAR, 
        today.get(Calendar.YEAR) + 1); 
    } 
    // Result in millis 

    final long millis = c.getTimeInMillis() 
         - today.getTimeInMillis(); 
    // Convert to days 
    final long days = millis/86400000; // Precalculated 
                // (24 * 
                // 60 * 
                // 60 * 
                // 1000) 
    // final String dayOfTheWeek = 
    // sdf.format(BDay.getTime()); 
    sdf = new SimpleDateFormat("EEEE"); 
    final String dayOfTheWeek = sdf.format(dt); 
    txt8.setText("" + days + " days"); 
    txt10.setText("" + dayOfTheWeek); 
} 

在這個方案的問題是剩下的作品今天的日期的基礎,但在剩下的日子不是從日期選擇器中選擇的日期,即意味着即使我從日期選擇器中選擇任何其他日期,其餘日期保持不變,並且日周也顯示相同,我希望兩者都根據所選日期進行更改

也用兩種不同的生日和輸出附接2個截圖是在我的屏幕截圖的最後兩行相同

enter image description here
enter image description here

我的程序正確地顯示了人的年齡選擇從日期選取任何日期時,以同樣的方式我想我剩下的日子,並在其上的人的生日,以改變一天當選擇從日期選取任何日期 請需要一些幫助,以什麼樣的變化,我需要在上面的代碼做任何 建議將是有益的 感謝您

回答

1

的問題是:

您正在計算使用當前日期的差異。

見少許修改功能:

private void calculateNextBirthday() { 
    // TODO Auto-generated method stub 

    /*Date dt = new Date(); //this is just current date, which was causing issue. It should be user selected date 
    SimpleDateFormat sdf = new SimpleDateFormat(
       "yyyy/MM/dd"); 
    final String strBDay = sdf.format(dt); 
    try { 

     dt = sdf.parse(strBDay); 
    } catch (final java.text.ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }*/ 

    final Calendar c = Calendar.getInstance(); 
    c.set(1990, 8, 03); //this is birthdate. set it to date you get from date picker 
    // c.add(Calendar.DATE, value); 
    final Calendar today = Calendar.getInstance(); 
    // Take your DOB Month and compare it to current 
    // month 
    final int BMonth = c.get(Calendar.MONTH); 
    final int CMonth = today.get(Calendar.MONTH); 

    final int BDate = c.get(Calendar.DAY_OF_MONTH); 
    final int CDate = today.get(Calendar.DAY_OF_MONTH); 

    c.set(Calendar.YEAR, today.get(Calendar.YEAR)); 
    c.set(Calendar.DAY_OF_WEEK, 
       today.get(Calendar.DAY_OF_WEEK)); 
    if (BMonth < CMonth) { 
     c.set(Calendar.YEAR, 
        today.get(Calendar.YEAR) + 1); 
    } 
    //added this condition so that it doesn't add in case birthdate is greater than current date . i.e. yet to come in this month. 
    else if (BMonth == CMonth){ 
     if(BDate < CDate){ 
      c.set(Calendar.YEAR, 
        today.get(Calendar.YEAR) + 1); 
     } 
    } 
    // Result in millis 

    final long millis = c.getTimeInMillis() 
         - today.getTimeInMillis(); 
    // Convert to days 
    final long days = millis/86400000; // Precalculated 
                // (24 * 
                // 60 * 
                // 60 * 
                // 1000) 
    // final String dayOfTheWeek = 
    // sdf.format(BDay.getTime()); 
    /*sdf = new SimpleDateFormat("EEEE"); 
    final String dayOfTheWeek = sdf.format(dt);*/ 
    Toast.makeText(getApplicationContext(), String.valueOf(days), Toast.LENGTH_SHORT).show(); 

} 

希望這有助於。

+0

先生感謝你剩下的日子完美的作品,但先生的日子並沒有改變,它總是顯示週三和爵士的剩餘天數正在改變它每週都發生變化,意味着像從2014年11月25日至2014年12月2日它顯示剩下的同一天,它只在一個星期後改變,但我想每天改變它,我知道它是因爲DAY_OF_WEEK,該怎麼做先生? – 2014-12-03 09:12:34

+0

你需要正確地通過日期。 @arjunnarahari – 2014-12-03 09:13:36

相關問題