Skip to content

Instantly share code, notes, and snippets.

@ani03sha
Created February 8, 2023 05:18
Show Gist options
  • Save ani03sha/2768d9377ffd42fb82c163deb6e63e7b to your computer and use it in GitHub Desktop.
Save ani03sha/2768d9377ffd42fb82c163deb6e63e7b to your computer and use it in GitHub Desktop.
Java program to convert seconds to years, months, days, hours, minutes, seconds, etc. Does not take care of corner cases like leap years and all.
public class ConvertSeconds {
public static void main(String[] args) {
ConvertSeconds convertSeconds = new ConvertSeconds();
System.out.println(convertSeconds.convert(129601));
System.out.println(convertSeconds.convert(86410));
}
public String convert(long inputSeconds) {
long year = inputSeconds / (365 * 24 * 60 * 60);
inputSeconds %= 365 * 24 * 60 * 60;
long month = inputSeconds / (30 * 24 * 60 * 60);
inputSeconds %= 30 * 24 * 60 * 60;
long days = inputSeconds / (24 * 60 * 60);
inputSeconds %= 24 * 60 * 60;
long hours = inputSeconds / (60 * 60);
inputSeconds %= 60 * 60;
long minutes = inputSeconds / 60;
inputSeconds %= 60;
long seconds = inputSeconds;
return year + " years, " + month + " months, " + days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment