0

I have made a class where I am going to store the duration of an event. As you see I have made fields for each but would it be better to save it as seconds and then calculate the hour, minute and seconds on the fly? The add method that I made would be simpler if I did so but are there any disadvantages?

public class Duration { private int hours; private int minutes; private int seconds; public Duration(int hours, int minutes, int seconds) { setHours(hours); setMinutes(minutes); setSeconds(seconds); } public int getHours() { return hours; } public void setHours(int hours) { this.hours = hours; } public int getMinutes() { return minutes; } public void setMinutes(int minutes) { this.minutes = minutes; } public int getSeconds() { return seconds; } public void setSeconds(int seconds) { this.seconds = seconds; } public void add(Duration duration) { } } 
6
  • Is this homework? are you required not to use external libraries? Commented Feb 20, 2012 at 18:06
  • See stackoverflow.com/questions/439903/… - you don't have to write this code yourself. Commented Feb 20, 2012 at 18:07
  • It would be better to store only seconds and calculate hours and minutes on demand. Otherwise, arithmetic will be harder. Also, you need range validation. Commented Feb 20, 2012 at 18:07
  • docs.oracle.com/javase/1.4.2/docs/api/java/sql/Timestamp.html Commented Feb 20, 2012 at 18:07
  • See Peter's answer. If you truly, truly want to make your own class it is indeed much better to just store it in (m)seconds and calculate everything else. You're going to introduce a lot of bugs otherwise. Commented Feb 20, 2012 at 18:09

3 Answers 3

4

It would be much better to use an existing library, such as Joda.

Sign up to request clarification or add additional context in comments.

2 Comments

All I need is just the time (hh:mm:ss). Like the length of an event.I don't care about year, timezones etc.
Check out the LocalTime class, joda-time.sourceforge.net/api-release/org/joda/time/… - do you really want to spend time on implementing time handling or on the actual logic of your application?
0

Unless you want to add additional jars to your distribution, I would just use java.util.Calendar.

Comments

0

Consider the java.util.Date class.

The trick is that a Date wraps a time stamp expressed as number of milliseconds elapsed from Jan 1st 1970, 12:00 AM GMT. This field can be obtain with the getTime() method.

To get the duration of an event, create two Date objects (start time and stop time) and then simply subtract the two time stamps (they are simply long values).

Then, to format it in terms of hours, minutes, seconds, etc., you can do something like:

 long milliseconds = elapsedInMilliseconds % 1000; long seconds = elapsedInMilliseconds / 1000 % 60; long minutes = elapsedInMilliseconds / (60 * 1000) % 60; long hours = elapsedInMilliseconds / (60 * 60 * 1000) % 24; long days = elapsedInMilliseconds / (24 * 60 * 60 * 1000); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.