0
私は、指定された将来の日付に何年、何ヶ月、および何日残っているかを表示するアンドロイドアプリを開発しようとしています。ただし、払い戻し値を取得しています。/D-Day Counter Java
ここで、2018/06/18までの残りの年、月、および日を計算します。残りの月の計算は、毎月の日数が異なる可能性があるためかなり難しかったです。だから私は残りの月を得ることを試みる間に間違いを犯したと信じています。しかし、私の結果はイライラさせられた:私が得た41年、01ヶ月、負振るう値日...
public void countDownStart() {
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, 1000);
long diff;
long years=0;
long months=0;
long days=0;
long temp_days=0;
int current_month;
Calendar futureDate = Calendar.getInstance();
futureDate.set(Calendar.DAY_OF_MONTH, 18);
futureDate.set(Calendar.MONTH, 6);
futureDate.set(Calendar.YEAR, 2018);
Calendar referenceDate = Calendar.getInstance();
Calendar currentDate = Calendar.getInstance();
if (futureDate.after(currentDate)) {
diff = futureDate.getTimeInMillis() - currentDate.getTimeInMillis();
days = diff/(1000*60*60*24);
years = diff/(1000 * 60 * 60 * 24 * 365);
diff = diff - years * 1000 * 60 * 60 * 24 * 365;
months = 0;
boolean flag = true;
current_month = referenceDate.get(Calendar.MONTH);
while (flag) {
switch (current_month) {
case 0:
case 2:
case 4:
case 6:
case 7:
case 9:
case 11:
diff = diff - 1000 * 60 * 60 * 24 * 31;
break;
case 1:
diff = diff - 1000 * 60 * 60 * 24 * 28;
break;
case 3:
case 5:
case 8:
case 10:
diff = diff - 1000 * 60 * 60 * 24 * 31;
break;
}
months = months + 1;
referenceDate.add(Calendar.MONTH, 1);
current_month=referenceDate.get(Calendar.MONTH);
temp_days = diff/1000 * 60 * 60 * 24;
if (temp_days <= 31) {
int next_month = referenceDate.get(Calendar.MONTH);
int days_in_month;
if (next_month == 0 || next_month == 2 || next_month == 4 || next_month == 6 ||
next_month == 7 || next_month == 9 || next_month == 11) {
days_in_month = 31;
} else if (next_month == 1) {
days_in_month = 28;
} else {
days_in_month = 30;
}
if (temp_days < days_in_month) {
flag = false;
//days = temp_days;
}
}
}
}
else
{
years=0;
months=0;
days=0;
}
txtTimerYear.setText(""+String.format("%02d",years));
txtTimerMonth.setText(""+String.format("%02d",months));
txtTimerDay.setText(""+String.format("%03d",days));
}
};
handler.postDelayed(runnable,1*1000);
}
}
は、私は本当にあなたの助けをいただければ幸いです! ありがとうございます!