此代码的问题在于它确实只为某些出生日期计算了活着的天数。我尝试使用不同的出生日期并使用在线计算器进行测试,但似乎并非所有都是正确的。我认为这个问题是因为Leapyears,一个月内不到30天。
public class DaysAlive {
public static void main (String [] args){
Scanner userInput = new Scanner(System.in);
int TodayYear, TodayMonth, TodayDay;
int YearBorn, MonthBorn, DayBorn;
int DaysAlive;
System.out.println("Enter today's date");
System.out.print ("Year: ");
TodayYear = userInput.nextInt ();
System.out.print ("Month: ");
TodayMonth = userInput.nextInt ();
System.out.print ("Day: ");
TodayDay = userInput.nextInt ();
System.out.println("Enter date of birth");
System.out.print ("Year: ");
YearBorn = userInput.nextInt ();
System.out.print ("Month: ");
MonthBorn = userInput.nextInt ();
System.out.print ("Day: ");
DayBorn = userInput.nextInt ();
//I think this line is the problem
DaysAlive = (TodayYear - YearBorn) *365 + (TodayMonth - MonthBorn) *30 +(TodayDay - DayBorn);
System.out.println("DaysAlive: " + DaysAlive);
}
}
答案 0 :(得分:3)
如何使用日历?它会为你做所有这些...或者joda时间库
答案 1 :(得分:2)
如果我理解你想要的东西,你应该可以使用这样的东西 -
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println("Enter today's date");
System.out.print("Year: ");
int todayYear = userInput.nextInt();
System.out.print("Month: ");
int todayMonth = userInput.nextInt();
System.out.print("Day: ");
int todayDay = userInput.nextInt();
// Java has January as month 0. Let's not require that the user know.
Calendar today = new GregorianCalendar(todayYear, todayMonth - 1,
todayDay);
System.out.println("Enter date of birth");
System.out.print("Year: ");
int yearBorn = userInput.nextInt();
System.out.print("Month: ");
int monthBorn = userInput.nextInt();
System.out.print("Day: ");
int dayBorn = userInput.nextInt();
Calendar born = new GregorianCalendar(yearBorn, monthBorn - 1, dayBorn);
double diff = today.getTimeInMillis() - born.getTimeInMillis();
diff = diff / (24 * 60 * 60 * 1000); // hours in a day, minutes in a hour,
// seconds in a minute, millis in a
// second.
System.out.println(Math.round(diff));
}
带输出
Enter today's date
Year: 2014
Month: 03
Day: 14
Enter date of birth
Year: 2014
Month: 03
Day: 13
1
答案 2 :(得分:1)
你是对的,问题是并非所有月份都有30天。一个简单的解决方案是使用if语句并创建另一个名为monthMultiplier的整数变量。正如其他人指出你也可以使用日历
if(TodayMonth == 9 || TodayMonth == 11 || TodayMonth == 4 || TodayMonth == 6) {
monthMultiplier = 30;//30 days in these months
}
if(TodayMonth == 2){
monthMultiplier = 28;//WELL 28.25 accounting for leap years
}
else {
monthMultiplier = 31;
}
DaysAlive = (TodayYear - YearBorn) *365 + (TodayMonth - MonthBorn) * monthMultiplier +(TodayDay - DayBorn);
答案 3 :(得分:1)
Joda-Time库可以轻松解决此问题。基本上只有一行代码。 Joda-Time处理时区,闰日和其他问题。
手动编码这样的日期时间工作是冒险的,容易出错,并且鉴于可用的优秀库(Joda-Time和Java 8中的新java.time包),坦率地说是愚蠢的。
以下是Joda-Time 2.3中的一些示例代码。
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime start = new DateTime(2008, 4, 26, 0, 0, 0, timeZone);
DateTime stop = new DateTime(2008, 5, 26, 0, 0, 0, timeZone);
int daysBetween = Days.daysBetween( start, stop ).getDays();
转储到控制台...
System.out.println( "start: " + start );
System.out.println( "stop: " + stop );
System.out.println( "daysBetween: " + daysBetween );
执行时......
start: 2008-04-26T00:00:00.000+02:00
stop: 2008-05-26T00:00:00.000+02:00
daysBetween: 30