First off a typical bad habit many beginners get into is, putting everything in main. If the code is only doing one thing and can be self contained a function will do. Otherwise, use a class. In this case I would suggest a class with static fields and functions.
Try to avoid using magic numbers, strings, etc.. Use final
variables so that they can be named. In many cases it can be difficult to figure out why a particular value was used, in a years time or longer.
There is a fair bit of repetition in your code. A helper function would clean that up.
Use formatted strings instead concatenation(+
).
Since you seem to be using one language for programming and a different one for output, I would deduce that it's possible that other languages may also be required for the output. to this end a simple TimeNames
class to store different string arrays with the time labels in different languages.
TimeNames.java
public class TimeNames{
public enum Languages{
English,
German
}
static final String[] germanNames = {
"Jahre",
"Tage",
"Stunden",
"Minuten",
"Sekunden"
};
static final String[] englishNames = {
"Years",
"Days",
"Hours",
"Minutes",
"Seconds"
};
static String[] names = englishNames;
public static void changeLanguage(Languages language){
switch(language){
case English:
names = englishNames;
break;
case German:
names = germanNames;
break;
default:
names = englishNames;
}
}
public static String getYears(){
return names[0];
}
public static String getDays(){
return names[1];
}
public static String getHours(){
return names[2];
}
public static String getMinutes(){
return names[3];
}
public static String getSeconds(){
return names[4];
}
}
SecondsToTime.java
public class SecondsToTime{
static final int SECS_IN_MINUTES = 60;
static final int MINS_IN_HOUR = 60;
static final int HOURS_IN_DAY = 24;
static final int DAYS_IN_YEAR = 365;
static final int MIN_CONV = SECS_IN_MINUTES;
static final int HOUR_CONV = MIN_CONV * MINS_IN_HOUR;
static final int DAY_CONV = HOUR_CONV * HOURS_IN_DAY;
static final int YEAR_CONV = DAY_CONV * DAYS_IN_YEAR;
static long years = 0;
static long days = 0;
static long hours = 0;
static long minutes = 0;
static long seconds = 0;
public static String secondsToTime(long secs){
long[] secsTotal = {secs};
years = convert(YEAR_CONV,secsTotal);
days = convert(DAY_CONV,secsTotal);
hours = convert(HOUR_CONV,secsTotal);
minutes = convert(MIN_CONV, secsTotal);
seconds = secsTotal[0];
return String.format("%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d", TimeNames.getYears(), years, TimeNames.getDays(), days,
TimeNames.getHours(), hours, TimeNames.getMinutes(), minutes, TimeNames.getSeconds(), seconds);
}
private static long convert (final int factor, long[] secs){
int temp = (int)secs[0]/factor;
secs[0] -= temp * factor;
return temp;
}
}
Main.java
public class Main{
public static void main(String[] args) {
String time = SecondsToTime.secondsToTime(1000000000);
System.out.println(time);
TimeNames.changeLanguage(TimeNames.Languages.German);
time = SecondsToTime.secondsToTime(1000000000);
System.out.println(time);
}
}