-1

Task: It is necessary to calculate the user's age, in days, hours, minutes. Enter a date of birth and today's date.

It seems like, age calculates correctly, but in days there is not the right figure. How to calculate the difference between dates in days?

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);

    } 
}

Found the necessary algorithm on Python. Tell me how to implement it in 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
Stas Iliukovich
  • 59
  • 3
  • 12
  • For the hours and minutes parts, you need more input lines. – Jongware Feb 24 '18 at 11:10
  • The number of days in a year are `365` and `366` check my answer – DobromirM Feb 24 '18 at 11:18
  • (a) **Search Stack Overflow before posting.** You can assume any basic date-time question has already been asked and answered, as has any basic homework assignment for a programming class. (b) **Put more effort in composing your Question.** In the title and the code example you work with whole dates, yet in the middle you ask about “days, hours, minutes”. Stack Overflow is intended to be more like Wikipedia and less like a casual discussion board/forum. So please take care in thinking through your issues and question. – Basil Bourque Feb 25 '18 at 19:44

3 Answers3

7

Use the API provided by Java:

LocalDate now = LocalDate.now();
LocalDate birthDate = LocalDate.of(1975, 7, 19);

long days = ChronoUnit.DAYS.between(birthDate, now);
System.out.println("days = " + days);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

You can calculate

eg:

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));
0

Your year_leap = 364; for non leap years should be equal to 365 and 366 for leap years.

This line day_month = month_day[1] * userMonth + userDay; is always using 29 as month_day[1] is always referring to February.

Also if it is not mandatory to do the calculations yourself look at the suggested API`s as they give you much better functionality.

DobromirM
  • 2,017
  • 2
  • 20
  • 29