关于算法:以天为单位计算用户的年龄

Calculate the user's age in days java

本问题已经有最佳答案,请猛点这里访问。

任务:需要计算用户的年龄,以天、小时、分钟为单位。输入出生日期和今天的日期。

似乎,年龄计算正确,但在几天内没有正确的数字。如何计算日与日之间的差额?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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中实现它?

1
2
3
4
5
6
7
8
9
10
11
12
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


使用Java提供的API:

1
2
3
4
5
LocalDate now = LocalDate.now();
LocalDate birthDate = LocalDate.of(1975, 7, 19);

long days = ChronoUnit.DAYS.between(birthDate, now);
System.out.println("days =" + days);

非闰年的year_leap = 364;等于闰年的365366

该行day_month = month_day[1] * userMonth + userDay;总是使用29,因为month_day[1]总是指二月。

此外,如果不强制自己进行计算,请查看建议的API,因为它们提供了更好的功能。


你可以计算

如:

1
2
3
4
Date date1 =
Date date2 =
long diff = date2.getTime() - date1.getTime();
System.out.println ("Days:" + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));

1
float days = (diff / (1000*60*60*24));