0

I am having trouble converting the time 1352364 in seconds to a time stamp format in "Weeks, Days, Hours, Seconds, Minutes". Here is my code so far.

int timeToGrow = 1352364;
    int secs = timeToGrow % 60;
    int mins = (timeToGrow / 60) % 60;
    int hours = (timeToGrow / 60) / 60;
    int days = ((timeToGrow / 60) / 60) / 24;
    int weeks = (((timeToGrow/60)/60)/24)/7;

    System.out.println(weeks + " Weeks "+ days + " Days " + hours + " Hours " + mins + " Minutes " + secs + " Seconds.");

The code should print out 2 Weeks 1 Days 15 Hours 39 Minutes 24 Seconds, but instead, in prints out 2 Weeks 15 Days 375 Hours 39 Minutes 24 Seconds. I think I see the problem, however I am not sure how to fix it. Excuse my poor wording, but I think that the hours section includes the time in the days and the weeks section, unlike where the time is seperate, like in minutes and seconds (again, sorry Im not sure how to word it). How can I change the formulas to correctly display the correct timestamp? Thank you very much!

J Boonders
  • 41
  • 5
  • 1
    If this isn't for an assignment, you should consider checking out the [Duration](https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html) class from java 8's time api. It has some helper methods for this. – killjoy Apr 17 '18 at 19:13
  • For the word usage, this isn’t a timestamp, it sounds more like a duration, an amount of time. A *timestamp* is information that identifies when an event occurred. – Ole V.V. Apr 17 '18 at 19:44

2 Answers2

4
int timeToGrow = 1352364;
int secs = timeToGrow % 60;
int mins = (timeToGrow / 60) % 60;
int hours = ((timeToGrow / 60) / 60) % 24;
int days = (((timeToGrow / 60) / 60) / 24) % 7;
int weeks = (((timeToGrow/60)/60)/24)/7;
System.out.println(weeks + " Weeks "+ days + " Days " + hours + " Hours " + mins + " Minutes " + secs + " Seconds.");
Dhruv Sehgal
  • 1,402
  • 1
  • 13
  • 15
4

This depends on which version of Java you're using.

Java 8 or later

You could use the Java Duration class:

 Duration duration = Duration.ofSeconds(1352364);

its APIs has methods to get you the number of nanoseconds, seconds, hours, days, etc. For example:

 System.out.println(duration.toDays() + " days");

To get the remainders you could use the methods .minusDays(), minusHours() and so on, or do something like:

 final int HOURS_IN_A_DAY = 24;
 System.out.println(duration.toHours() % HOURS_IN_A_DAY);

Java 9 or later

Use the Duration class as explained above; but with newer versions of Java, this class has methods for the proper remainders, and you can generate your string as follows:

System.out.println(
    duration.toDaysPart() + " days, " +
    duration.toHoursPart() + " hours, " +
    duration.toMinutesPart() + " minutes, " + 
    duration.toSecondsPart() + " seconds."
);

... but this is without weeks. If you want weeks, get the days, set final int DAYS_IN_A_WEEK = 7, and use days / DAYS_IN_A_WEEK and days % DAYS_IN_A_WEEK.

Java 7 or earlier

If you're working with earlier versions of Java, use the backport of the newer-Java-versions java.time classes (thanks @OleVV for the suggestion), or the Joda Time classes - both of these have a Duration class, and Joda has a PeriodFormatter class, as described in this answer.

If you're using Java 5 or earlier, only Joda will work.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Good answer. For Java 6 and 7 I’d prefer [the ThreeTen Backport](http://www.threeten.org/threetenbp/), the backport of `java.time`, over Joda-Time. For Java 5 Joda-Time is the right choice. – Ole V.V. Apr 17 '18 at 19:42
  • Thanks for the answer, however I am unsure of how to correctly use duration. – J Boonders Apr 18 '18 at 18:14
  • 1
    @JBoonders For how to use `Duration`, I think the answer gives a nice demonstration. You may also visit [the Java date and time tutorial](https://docs.oracle.com/javase/tutorial/datetime/), in particular [the section on Period and Duration](https://docs.oracle.com/javase/tutorial/datetime/iso/period.html). And search the net. Finally [the `Duration` class documentation](https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html). – Ole V.V. Apr 18 '18 at 20:30