1 package tim.prune.data;
3 import tim.prune.I18nManager;
6 * Class to represent a time difference, like the difference between two Timestamp objects,
7 * and methods for representing and displaying them.
9 public class TimeDifference
11 private long _totalSeconds = 0L;
12 private int _seconds = 0;
13 private int _minutes = 0;
14 private int _hours = 0;
15 private String _description = null;
19 * Constructor using long
20 * @param inNumSeconds number of seconds time difference
22 public TimeDifference(long inNumSeconds)
24 _totalSeconds = inNumSeconds;
25 if (inNumSeconds < 0) {inNumSeconds = -inNumSeconds;}
26 _hours = (int) (inNumSeconds / 60 / 60);
27 _minutes = (int) (inNumSeconds / 60 - _hours * 60);
28 _seconds = (int) (inNumSeconds % 60);
33 * Constructor giving each field separately
34 * @param inHours number of hours
35 * @param inMinutes number of minutes
36 * @param inSeconds number of seconds
37 * @param inPositive true for positive time difference
39 public TimeDifference(int inHours, int inMinutes, int inSeconds, boolean inPositive)
41 // Check for negative values?
45 _totalSeconds = inHours * 3600L + inMinutes * 60L + inSeconds;
46 if (!inPositive) {_totalSeconds = -_totalSeconds;}
51 * @return total number of seconds time difference
53 public long getTotalSeconds()
59 * @return number of hours
61 public int getNumHours()
67 * @return number of minutes
69 public int getNumMinutes()
75 * @return number of seconds
77 public int getNumSeconds()
83 * @return true if time difference positive
85 public boolean getIsPositive()
87 return _totalSeconds >= 0L;
92 * Build a String to describe the time duration
93 * @return time as a string, days, hours, mins, secs as appropriate
95 public String getDescription()
97 if (_description != null) {return _description;}
98 StringBuffer buffer = new StringBuffer();
99 boolean started = false;
103 buffer.append(_hours).append(' ').append(I18nManager.getText("display.range.time.hours"));
109 if (started) {buffer.append(", ");}
110 else {started = true;}
111 buffer.append(_minutes).append(' ').append(I18nManager.getText("display.range.time.mins"));
114 if (_seconds > 0 || !started)
116 if (started) {buffer.append(", ");}
117 buffer.append(_seconds).append(' ').append(I18nManager.getText("display.range.time.secs"));
119 _description = buffer.toString();