任何人都可以帮助我如何计算DOB的下一个生日剩余天数以及生日,即周一或周二等生日
答案 0 :(得分:1)
例如,要获取星期几,您需要输入:
Calendar daymonthyear = Calendar.getInstance();
daymonthyear.set(birthdayYear, birthdayMonth, birthdayDay);
int dayOfTheWeek = daymonthyear.get(Calendar.DAY_OF_WEEK);
编辑:通过简单减去当前日/月/年和生日日/月/年值来获得剩余天数。 但是,请注意不要在公式中留下负值
答案 1 :(得分:1)
[更新代码]
添加以下导入:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
final String strBDay = "2014/04/07";//"1990/04/07"; // Next Birtday date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date dt = null;
try
{
dt = sdf.parse(strBDay);
}
catch (final java.text.ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
final Calendar BDay = Calendar.getInstance();
BDay.setTime(dt);
final Calendar today = Calendar.getInstance();
// Result in millis
final long millis = BDay.getTimeInMillis() - today.getTimeInMillis();
// Convert to days
final long days = millis / 86400000; // Precalculated (24 * 60 * 60 * 1000)
sdf = new SimpleDateFormat("EEEE");
sdf.format(dt);
System.out.println("Left days to bday: " + days);
System.out.println("It will be : " + days);
输出:
12-14 16:23:08.811:I / System.out(5253):剩下的日子到星期日:113
12-14 16:23:08.811:I / System.out(5253):它将是:星期一
[使用D.O.B的版本]
如果你想使用你的出生日期,那就是这样:
final String strBDay = "1990/04/07";//"1990/04/07"; // Next Birtday date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date dt = null;
try
{
dt = sdf.parse(strBDay);
}
catch (final java.text.ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
final Calendar BDay = Calendar.getInstance();
BDay.setTime(dt);
final Calendar today = Calendar.getInstance();
// Take your DOB Month and compare it to current month
final int BMonth = BDay.get(Calendar.MONTH);
final int CMonth = today.get(Calendar.MONTH);
BDay.set(Calendar.YEAR, today.get(Calendar.YEAR));
if(BMonth <= CMonth)
{
BDay.set(Calendar.YEAR, today.get(Calendar.YEAR) + 1);
}
// Result in millis
final long millis = BDay.getTimeInMillis() - today.getTimeInMillis();
// Convert to days
final long days = millis / 86400000; // Precalculated (24 * 60 * 60 * 1000)
sdf = new SimpleDateFormat("EEEE");
//final String dayOfTheWeek = sdf.format(BDay.getTime());
final String dayOfTheWeek = sdf.format(dt);
System.out.println("Left days to bday: " + days);
System.out.println("It will be : " + dayOfTheWeek);
输出:
12-14 16:48:11.801:I / System.out(5342):剩下的日子到星期日:113
12-14 16:48:11.801:I / System.out(5342):它将是:星期一