Hi all

Is there any class to store time in easily, without having to store a date too?

We have to create a program for swimming competitions. The swimmers in a match need a time (the time they did swimming the match). So we only need hours, minutes, seconds and milliseconds.

Edit start:
I found the class Time too, but there you have to give in milliseconds since a certain date, which is not easy...
The constructor Time(int hour, int minute, int second) is deprecated...
Edit end.

Thanks in advance
Kenny

Ps: this may seem like a stupid question, but this is an example I found:

import java.util.*;
import java.text.*;
public class Apollo {
   public static void main(String[] args) {
      GregorianCalendar liftOffApollo11 = new GregorianCalendar(1969, Calendar.JULY, 16, 9, 32);
      Date d = liftOffApollo11.getTime();
      DateFormat df1 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
      DateFormat df2 = DateFormat.getTimeInstance(DateFormat.SHORT);
      String s1 = df1.format(d);
      String s2 = df2.format(d);
      System.out.println(s1);
      System.out.println(s2);
   }
}

and here you need to give in a date too.

Dani AI

Generated

Quick update for — and a nod to : a small value type is a good idea, but modern Java already gives a clean option for race times (elapsed durations) and a separate option for clock-of-day values.

If you want to represent an elapsed race time, use java.time.Duration (Java 8+). It models an amount of time with sub-second precision, is Comparable (easy to sort/compare), and can be composed from hours/minutes/seconds/milliseconds or parsed in ISO-8601 form. See the API for details: java.time.Duration docs.

Example (construct and format as mm:ss.SSS):

Duration t = Duration.ofMinutes(1).plusSeconds(23).plusMillis(456);

long ms = t.toMillis();
long mins = ms / 60000;
long secs = (ms % 60000) / 1000;
long millis = ms % 1000;
String display = String.format("%02d:%02d.%03d", mins, secs, millis);

If you instead need a time-of-day (a clock reading like "16:05"), use java.time.LocalTime: LocalTime docs.

If stuck on an older JDK (pre‑8), consider Joda-Time as a stable alternative: Joda-Time. For persistence, store a canonical form (numeric milliseconds or the ISO-8601 duration string) so formatting/parsing is one-way and locale-independent.

Bottom line: for swimmer lap times use Duration; it avoids inventing ad hoc classes and handles precision, comparison, and simple formatting cleanly.

Recommended Answers

All 4 Replies

You might want to create your own class.

The classes Date and Time are used to store what the say: Date and Time.
You need to store duration. (duration of race)
You cannot say that a swimmer swimmed for 'Date' object time.

But you can say this:He started swimming at Date start = new Date() and finished at Date finish = new Date() And their difference is the time he made in milliseconds: long time = end.getTime() - start.getTime()

Ok thanks, that's a possible solution.
But isn't there already a class in java that can store just an hour, minutes, seconds and milliseconds?
Edit: Or just minutes, seconds and milliseconds, cause a swimmer probably won't be swimming for an hour or more

Cause it's not actually known, or put in by the user, when the swimmer started, only a time is entered.

I could make a class that can store 4 Integers, but a prefab class would be handy.

Thx, Kenny.

I don't believe there is a class that does what you want.

Also I checked the Time class and I don't believe it is what you want. It is used with sql and it represents time like saying now the time is:
"16:05"

Ok, pitty, but thanks for the help.
Greets, Kenny

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.