任务:有必要计算用户的年龄,以天,小时,分钟为单位。输入出生日期和今天的日期。
似乎,年龄计算正确,但在几天内没有正确的数字。如何计算日期之间的差异?
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int day;
int year;
int month;
int day2; //today
int year2; //today's year
int month2; //today's month
do {
System.out.print("Enter the year of birth: ");
year = scanner.nextInt();
} while (year < 1000 || year > 2019);
do {
System.out.print("Enter the month of birth: ");
month = scanner.nextInt();
} while (month < 1 || month > 12);
do {
System.out.print("Enter the day of birth: ");
day = scanner.nextInt();
} while (day < 1 || day > 31);
do {
System.out.print("Enter today's year: ");
year2 = scanner.nextInt();
} while (year2 < 1000 || year2 > 2019);
do {
System.out.print("Enter today's month: ");
month2 = scanner.nextInt();
} while (month2 < 1 || month2 > 12);
do {
System.out.print("Enter today: ");
day2 = scanner.nextInt();
} while (day2 < 1 || day2 > 31);
int year_leap;
int[] month_day = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
year_leap = 366;
month_day[1] = 29;
} else {
month_day[1] = 28;
year_leap = 365;
}
int userYear = 0;
int userMonth;
int userDay;
if (year <= year2) {
userYear = year2 - year;
} else System.out.printf("Error!!! %n");
if (month > month2) {
userYear--;
userMonth = month2 + 12 - month;
} else userMonth = month2 - month;
if (day > day2) {
userMonth--;
userDay = day2 + month_day[userMonth] - day;
} else userDay = day2 - day;
int day_year = 0;
int day_month = 0;
int day_age = 0;
while (year < year2 - 1) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
year_leap = 366;
month_day[1] = 29;
} else {
year_leap = 365;
month_day[1] = 28;
}
year++;
day_year = day_year + year_leap;
}
day_month = month_day[1] * userMonth + userDay; // 31 days in a month
day_age = day_year + day_month;
System.out.printf("------------------------------------ %n");
System.out.println("Your age: " + userYear + "." + userMonth + "." + userDay);
System.out.println("Your in days:" + day_age);
}
}
在Python上找到必要的算法。告诉我如何用Java实现它?
def dateIsBefore(year1, month1, day1, year2, month2, day2):
return (year1, month1, day1) < (year2, month2, day2)
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
assert not dateIsBefore(year2, month2, day2, year1, month1, day1)
days = 0
while dateIsBefore(year1, month1, day1, year2, month2, day2):
days += 1
year1, month1, day1 = nextDay(year1, month1, day1)
return days
答案 0 :(得分:3)
使用Java提供的API:
LocalDate now = LocalDate.now();
LocalDate birthDate = LocalDate.of(1975, 7, 19);
long days = ChronoUnit.DAYS.between(birthDate, now);
System.out.println("days = " + days);
答案 1 :(得分:0)
你可以计算
例如:
Date date1 =
Date date2 =
long diff = date2.getTime() - date1.getTime();
System.out.println ("Days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
OR
float days = (diff / (1000*60*60*24));
答案 2 :(得分:0)
对于非闰年,year_leap = 364;
应该等于365
和366
闰年。
此行day_month = month_day[1] * userMonth + userDay;
始终使用29
,因为month_day[1]
始终指的是二月。
此外,如果不是必须自己进行计算,请查看建议的API,因为它们可以为您提供更好的功能。