Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>--- Actually there's a much better way to do this, but if you want to use dates, skip to the pre-edit answer ---</p> <p>Dates don't actually do what you want, which appears to be time calculations outside of an actual need to pick times off a real world calendar.</p> <p>You'd be much better off writing your own class, to avoid all the nasty special handling that Dates must do in order to keep up with the Gregorian Calendar. This special handling includes (but is not limited to) timezone awareness, daylight savings, declared "skipped days", leap seconds, leap years, etc.</p> <pre><code>public TimeOnly { private long timestamp; private int millis; private int seconds; ... etc ... public TimeOnly(int hours, int minutes, int seconds, int millis) { this.timestamp = millis + seconds * 1000L + minutes * 60000L + hours * 3600000L; this.millis = millis; this.seconds = seconds; ... etc ... } private TimeOnly(long timestamp) { this.timestamp = timestamp; this.millis = timestamp % 1000; this.seconds = timestamp % 60000L - this.millis; ... etc ... } public long getTimestamp() { return timestamp; } public int getMillis() { return millis; } public int getSeconds() { return seconds; } ... etc ... } public TimeFormatter { public TimeFormatter() { } public String format(Time time) { StringBuilder builder = new StringBuilder(); builder.append(String.valueOf(time.getHours())); builder.append(":"); builder.append(String.valueOf(time.getMinutes())); builder.append(":"); builder.append(String.valueOf(time.getSeconds())); builder.append("."); if (time.getMillis() &lt; 10) { builder.append("00"); } else if (time.getMillis() &lt; 100) { builder.append("0"); } builder.append(time.getMillis()); return builder.toString(); } </code></pre> <p>This solution may seem like it is reinventing the wheel, but really it is avoiding the use of an octagon as a wheel. Date's behavior doesn't seem to be what you want, although you could possibly make Date work for some limited range of values.</p> <p>If you want to get really fancy, you could make the above implement comparable, etc. However, I would advise against on thing. Don't provide update methods after construction, as this forces some pretty nasty recalculations and makes the code harder to maintain. Instead provide methods that return new TimeOnlys in response to the operations you wish to implement.</p> <pre><code>public TimeOnly addSeconds(int value) { int stamp = this.timestamp; stamp += value * 60000L; if (stamp &lt; timestamp) { throw new Excepton("overflow"); } return new TimeOnly(stamp); } </code></pre> <p>Also, don't implement what you aren't going to use. Unused code tends to be fertile soil for bugs.</p> <p>And of course, the stock answer for all "time" things, consider using JodaTime, which differentiates all the different types of time measurement. However, for just a small problem like this, it's akin to using a tank to kill an ant.</p> <p>--- The pre-edit answer ---</p> <p>Without a full specification of the time (year, month, day, hour, minute, second, milliseconds) your time value as formatted in the first step will have lots of fields that are not specified. What goes in those fields will likely be garbage.</p> <p>Then <code>getTime()</code> acts on the entire <code>Date</code> object translating both the valid fields and the garbage into a value, where the garbage may even modify the valid values (96 sec = 1 minute and 36 seconds, as the fields interact).</p> <p>The best way to go about this is to have all your "time only" Dates initialized to one known day, so when you do comparisons and math operations (is, <code>3 11 23</code> > <code>1 02 10</code> ?) you get consistent results (yes, <code>3 11 23</code> > <code>1 02 10</code>, because it is actually <code>2013 02 10 00 03 11 23</code> > <code>2013 02 10 00 03 11 23</code> and not <code>2013 02 10 00 03 11 23</code> compared to <code>2000 02 10 00 03 11 23</code></p> <p>When selecting the day to use, avoid days adjacent to Feb 29th, days that are near daylight savings shifts, etc.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload