我已经编写了一个程序来根据从日期选择器中选择的日期计算一个人的年龄,并且其工作正常。 现在我添加了另一种方法来显示剩下的日子和人生日下一个生日那天
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);
}
此计划中的问题是剩余的剩余天数是根据今天的日期而不是从日期选择器中选择的日期,即使我从日期选择器中选择任何其他日期,剩余天数保持不变dayoftheweek也显示相同,我希望两者都根据所选日期进行更改
还附上两个不同生日的2个截图,截屏的最后两行输出相同
我的程序在从日期选择器中选择任何日期时正确显示了此人的年龄,就像我希望我的剩余日期和人生日所在的日期更改一样,当选择任何日期时日期选择器 请关于我在上面的代码中需要做些什么更改需要一些帮助 任何的意见都将会有帮助 感谢你
答案 0 :(得分: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();
}
希望这有帮助。