2015-06-29 2 views
0

, когда я пытаюсь вычислить дни рождения для всегда дает мне неправильное число днейAndroid Datepicker расчета дней до дня рождения

Можете ли вы помочь мне, что случилось?

Calendar call1 = Calendar.getInstance(); 
    Calendar call2 = Calendar.getInstance(); 
    int day2 = call2.get(Calendar.DAY_OF_MONTH); 
    int month2 = call2.get(Calendar.MONTH); 
    int year2 = call2.get(Calendar.YEAR); 


    if (month2 > month ) { 

    } 

    else if (month == month2){ 

     if(year2 < day){ 

     } 
    }call1.set(day2, month , day); 
    long millis = call2.getTimeInMillis() 
      - call1.getTimeInMillis(); 
      long days = millis/86400000L; 

    Toast.makeText(this.getApplicationContext(), "The number of days until your birthday" 
      + days, Toast.LENGTH_SHORT).show(); 
    text2.setText(""+ days); 
    text2.setTextColor(Color.parseColor("#990000")); 

Это первая часть кода:

SharedPreferences dp2 = this.getSharedPreferences("dp", 0); 

    int day = dp2.getInt("day", 0); 
    int month = dp2.getInt("month", 0) + 1; 
    int year = dp2.getInt("year", 0); 

    text1.setText(day + "/" + month + "/" + year); 
+1

https://github.com/dlew/joda-time-android. Используйте время Джоды. Дата между диапазоном http://stackoverflow.com/questions/3802893/number-of-days-between-two-dates-in-joda-time – Raghunandan

ответ

1

Используйте следующий метод, чтобы найти разницу между двумя датами.

/** 
* Get a diff between two dates 
* @param date1 the oldest date 
* @param date2 the newest date 
* @param timeUnit the unit in which you want the diff 
* @return the diff value, in the provided unit 
*/ 
public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) { 
    long diffInMillies = date2.getTime() - date1.getTime(); 
    return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS); 
} 

И тогда вы можете позвонить:

getDateDiff(date1,date2,TimeUnit.MINUTES); 

получить диф из 2-х дат минут единицы.

TimeUnitjava.util.concurrent.TimeUnit, стандартное перечисление Java, происходящее от наносов до дней.

Смежные вопросы