How Time Flies Help

Hello

The code passes all of the testcases, but unfortunatly only passes about half of the tests once submitted.

I would very much appreciate some guidence as to how to improve my solution.

I’m a beginner programmer, so I apologize for my messy code.

Here it is below: (in Java)

import java.util.*;

import java.io.*;

import java.math.*;

class Solution 

{

    private static int[] daysInEachMonth;

    public static int numDays;

    public static int numMonths;

    public static int numYears;

    public static int startDay;

    public static int startMonth;

    public static int startYear;

    public static int endDay;

    public static int endMonth;

    public static int endYear;

    public static int totalDays;

    public static String BEGIN;

    public static String END;

    //converts string to int

    public static int toInt(String str)

        {

            int newInt = Integer.parseInt(str);

            return newInt;

        }

    //adds s if plural and removes text if difference is 0

    public static String updateString(String date, int number)

    {

        if(number > 1)

        {

            date += "s";

        }

        else if(number == 0)

        {

             return "";

        }

        return number + " " + date + ", ";

    }

//adds number of days in each month to corresponding spot in array; ex: 1 = 31, 2 = 28

    private static void addDays()

    {

        for(int i = 0 ; i < 12; i++)

        {

            daysInEachMonth[i] = 31;

            if(i == 3 || i == 5 || i == 8 || i == 10)

            {

                daysInEachMonth[i] = 30;

            }

            else if(i == 1)

            {

                daysInEachMonth[i] = 28;

            }

            

        }

    }

    private static void calculateYears()

    {

        numYears = 0;

        

        for(int y = startYear; y < endYear; y++)

        {

            numYears++;

        }

        if(startMonth > endMonth)

        {

            numYears--;

        }

        else if(startMonth == endMonth && startDay > endDay)

        {

            numYears--;

            numMonths = 12;

        }

    }

    public static void calculateMonths()

    {

        if(startDay != endDay)

        {

            if(endDay > startDay)

            {

                numMonths++;

            }

            else

            numMonths--;

        }

        for(int m = startMonth; m != endMonth; m++)

        {

            if(m == 12)

            {

                m = 0;

            }

            numMonths++;

        }

    }

    public static void calculateDays()

    {

        numDays = 0;

        if(startDay != endDay)

        {

            numDays = daysInEachMonth[startMonth - 1] - startDay;

            numDays += endDay;

        }

    }

    public static void calculateTotalDays()

    {

        totalDays = 0;

        totalDays += numYears * 365;

        totalDays += numDays;

        for(int x = startYear; x <= endYear; x++)

        {

            if(x % 4 == 0 && !BEGIN.equals(END))

            {

                if(startMonth <= 2 && x == startYear)

                {

                    totalDays++;

                }

                else if(startMonth >= 2 && x == endYear)

                {

                    totalDays++;

                }

                else if(x != startYear && x != endYear)

                {

                    totalDays++;

                }

            }

        }

        if(startDay != endDay)

        {

            if(startDay > 1)

            {

                startMonth++;

                if(startMonth > 12)

                {

                    startMonth = 1;

                }

            }

            else if(endMonth < daysInEachMonth[endMonth - 1])

            {

                endMonth--;

                if(endMonth < 1)

                {

                    endMonth = 12;

                }

            }

        }

            for(int i = startMonth; i != endMonth; i++)

            {

                totalDays += daysInEachMonth[i - 1];

                if(i == 12)

                {

                    i = 1;

                    totalDays += daysInEachMonth[i - 1];

                }

            }

    }

    public static void getNumbers()

    {

        startDay = toInt(BEGIN.substring(0,2));

        startMonth = toInt(BEGIN.substring(3,5));

        startYear = toInt(BEGIN.substring(6));

        endDay = toInt(END.substring(0,2));

        endMonth = toInt(END.substring(3,5));

        endYear = toInt(END.substring(6));

    }

    public static void main(String args[]) {

        String year = "year";

        String month = "month";

        String day = "day";

        numMonths = 0;

        Scanner in = new Scanner(System.in);

        BEGIN = in.next();

        END = in.next();

        getNumbers();

        daysInEachMonth = new int[12];

        addDays();

        calculateYears();

        calculateMonths();

        calculateDays();

        calculateTotalDays();

        System.out.println(updateString(year, numYears) + updateString(month, numMonths) + "total " + totalDays + " days");

    }

}

As a java programmer, you should never manually calculate dates and leap years like what you are doing.
You can make good use of the built-in Calendar and similar classes for date/time calculations.

2 Likes

okay, thanks!
That made solving this puzzle a bit easier