Convert light-years to other representations the code is very messy, long and I am looking how to shorten it and/or simplify it. There are a lot of repeated variables, for example; 'hourToDay' and then 'hourToDay1', 'hourToDay2' and so forth. Can you help me find a better way to make this program more efficient?

P.S - I am a new coder and wanting to improve my ability, any suggestions would be grand :)

long lightYear = 6000000000000L;
int hoursInDay = 24;
int daysInYear = 365;
int yearsInCentury = 100;
int yearsInMillenium = 1000;


Scanner lightYearInput = new Scanner(System.in);
System.out.println("Enter light year's you'd like to convert" );

long light = lightYearInput.nextLong();
long totalLightYear = (long) (lightYear * light);



// Different vehicle variables with speed.

double car  = 277.87; //mph koenigseggAgeraRS
double MiG25 = 1919.42; //mph
double parkerSpaceProbe = 213240; //mph


// car conversion
long hourConversion = (long)(totalLightYear / car );
System.out.println("Driving the fastest car in space, would take you " + hourConversion + " hours to travel " + light + " light years."); //hours

long hourToDay = hourConversion / hoursInDay;
System.out.println(hourConversion + " hours is equivalent to " + hourToDay + " days."); //hours to days

long dayToYear = hourToDay / daysInYear;
System.out.println(hourToDay +" is equivalent to " + dayToYear + " years."); //days to years

long yearToCentury = dayToYear / yearsInCentury;
System.out.println(dayToYear +" is equivalent to " + yearToCentury + " Centuries."); //years to centuries

long yearToMillenium = yearToCentury / yearsInMillenium;
System.out.println(yearToCentury +" is equivalent to " + yearToMillenium + " millenium.\n"); // centuries to millenium

//MiG25 Conversion

System.out.println("PLANE");
long hourPlaneConversion = (long)(totalLightYear / MiG25 );
System.out.println("Driving the fastest plane in space, would take you " + hourPlaneConversion + " hours to travel " + light + " light years."); //hours

long hourToDay1 = hourPlaneConversion / hoursInDay;
System.out.println(hourPlaneConversion + " hours is equivalent to " + hourToDay1 + " days."); //hours to days

long dayToYear1 = hourToDay1 / daysInYear;
System.out.println(hourToDay1 +" is equivalent to " + dayToYear1 + " years."); //days to years

long yearToCentury1 = dayToYear1 / yearsInCentury;
System.out.println(dayToYear1 +" is equivalent to " + yearToCentury1 + " Centuries."); //years to centuries

long yearToMillenium1 = yearToCentury1 / yearsInMillenium;
System.out.println(yearToCentury1 +" is equivalent to " + yearToMillenium1 + " millenium.\n"); // centuries to millenium


The output works correctly (unless my maths / code is incorrect), so the program runs fine. I'm just simply looking to make my code more efficient. I'm sure you can see that I need to improve my ability, hopefully I can get some supportive guidance here.
Constants should be Constant

You shouldn't be able to change constants; they should be fixed, non-modifiable values.  In Java, this means declaring them final.

If you create two instances of your class, you'll end up with two instances of lightYear, hoursInDay and so on.  But since these values are fixed compile-time constants, it doesn't make sense to have multiple copies of them.  You just need one copy of these constants, which means declaring them as static.

Things like hours in a day, and days in a year are also fairly general values.  They could be of use in other classes, so it makes sense to make these values sharable, which means public.

Finally, convention is to name constants with an UPPERCASE_NAME.

So ...

public final static long MILES_PER_LIGHT_YEAR = 5878625541248L;

public final static int  HOURS_PER_DAY = 24;
public final static int  DAYS_PER_YEAR = 365;
public final static int  YEARS_PER_CENTURY = 100;
public final static int  YEARS_PER_MILLENNIUM = 1000; // Note: 2 N's


Time Conversion

As noted by @Sixsmith, the Hours to Days, Years, Centuries, and Millennium calculation is being repeated.  Unfortunately, they went a little far and added System.out.println("Driving the fastest car in space to their hoursConversion() function, so it is not general enough to use for the plane (or eventually, space probe).

Reducing the conversion code slightly produces a reusable function:

public static void output_hour_equivalents(long hours) {

    long days = hours / HOURS_PER_DAY;
    if (days &gt; 0)
        System.out.println(hours + " hours is equivalent to " + days + " days.");

    long years = days / DAYS_PER_YEAR;
    if (years &gt; 0)
        System.out.println(days + " days is equivalent to " + years + " years.");

    long centuries = years / YEARS_PER_CENTURY;
    if (centuries &gt; 0)
        System.out.println(years + " years is equivalent to " + centuries + " years.");

    long millennia = years / YEARS_PER_MILLENNIUM;
    if (millennia &gt; 0)
        System.out.println(years + " years is equivalent to " + millennia + " millennia.");
}


You could, of course, add "weeks" and "months" to this without loss of generality.  I added tests at each conversion to avoid printing out a zero duration if given a small number of hours, like output_hour_equivalents(10000).

Duration for distance at speed

The conversion from speed and lightyears, for the fastest class of an object, to duration can be moved into a general purpose function.  But we don't want to hard-code too much text.  We want to fly a plane, not drive one, so we should take in the object class, and the action we perform with it, as well as it's maximum speed and the distance to be traversed:

public static void describe_fastest(String object, String action, double max_speed, long light_years) {
    long miles = light_years * MILES_PER_LIGHT_YEAR;
    long hours = (long)(miles / max_speed);

    // Capitalize the first letter of our action, for output. 
    action = action.substring(0, 1).toUpperCase() + action.substring(1);

    System.out.println(action + " the fastest " + object + " in space, would take you " + hours + " hours to travel " + light_years + " light years.");
    output_hour_equivalents(hours);
}


You can now use this in your method.  For example, from main, borrowing @Sixsmith's variable names:

public static void main(String[] args) {

    long light_years = ...

    describe_fastest("car", "driving", carMph, light_years);
    describe_fastest("plane", "flying", planeMph, light_years);
    describe_fastest("space probe", "traveling in", spaceProbeMph, light_years);
}


1 Light Year

If you provide the value 1 for light_years you'll get something like:


  Driving the fastest car in space ... to travel 1 light years.


I hate that "1 light years."  It should be "1 light year."  Singular.  MessageFormat can help us here:

private final static String MSG_FORMAT =
    "{0} the fastest {1} in space, would take you {2} hours to travel " +
    "{3} {3,choice,0#light years|1#light year|1&lt;light years}.";

public static void describe_fastest(String object, String action, double max_speed, long light_years) {
    long miles = light_years * MILES_PER_LIGHT_YEAR;
    long hours = (long)(miles / max_speed);

    // Capitalize the first letter of our action, for output. 
    action = action.substring(0, 1).toUpperCase() + action.substring(1);

    System.out.println(MessageFormat.format(MSG_FORMAT, action, object, hours, light_years));
    output_hour_equivalents(hours);
}


The format string constant is private, since it is unlikely that anyone else would re-use that string anywhere.

You can also add commas between groups of 3 digits, and other things to make the output a little more pleasing.  See MessageFormat for details.

You can also make it output "hour" or "hours", "day" or "days", "year" or "years", "century" or "centuries", and "millennium" or "millennia" in the other messages.

Exercise left to student.