Do we have a TimeSpan sort of class in Java
Asked Answered
B

4

59

I was just wondering if there is a need of TimeSpan in java.util so that I can define how much hours,minutes and seconds are there in between these two times.

From this TimeSpan we can have a time interval between two times. like

TimeSpan getTimeSpan( Date before, Date after ){...}

or

long timeSpan = System.currentTimeMillis();
// ... long job
timeSpan = System.currentTimeMillis() - timeSpan;

TimeSpan ts = new TimeSpan(timeSpan);

and with this TimeSpan we can use it in SimpleDateFormat.

SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
format.format( timsSpan );

I am not sure if this is already been implemented in Java but yet undiscovered by me.

Bestraddle answered 5/7, 2011 at 11:2 Comment(0)
T
39

With JDK 8 date-time libraries in SDK has been enriched and you can use

Duration or Period

Interval from JodaTime will do..

A time interval represents a period of time between two instants. Intervals are inclusive of the start instant and exclusive of the end. The end instant is always greater than or equal to the start instant.

Intervals have a fixed millisecond duration. This is the difference between the start and end instants. The duration is represented separately by ReadableDuration. As a result, intervals are not comparable. To compare the length of two intervals, you should compare their durations.

An interval can also be converted to a ReadablePeriod. This represents the difference between the start and end points in terms of fields such as years and days.

Interval is thread-safe and immutable.

Threeply answered 5/7, 2011 at 11:4 Comment(4)
I like the JodaTime's Interval. But I wonder why not in standard library has yet came up with such utility. Since the usage is very common.Bestraddle
May be joda time will be added in standard lib soonThreeply
This actually happend, see my answer below and the documentation that answers this question here: docs.oracle.com/javase/8/docs/api/java/time/Period.htmlOrnelas
Nice to see that happened and I envisioned it in 2011 :)Threeply
T
30

In Java 8 a proper time library has been added to the standard API (this is based heavily on JodaTime).

In it there are two classes that you can use to indicate a period:

  1. Duration which indicates a seconds or nanoseconds length of a timespan.
  2. Period which indicates a more user-friendly difference, stored as 'x years and y months etc'.

A detailed explanation of the difference between them can be found in the Java tutorial

Tusche answered 22/6, 2015 at 11:23 Comment(0)
O
4

If you're on on Java 8 (or higher) or simply don't want to import JodaTime (the Author of JodaTime himself suggest migrating to java.time): Java 8 offers that functionality with Periods, see here for a tutorial: https://docs.oracle.com/javase/tutorial/datetime/iso/period.html

Let me quote the Oracle tutorial here:

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
                   " months, and " + p.getDays() +
                   " days old. (" + p2 + " days total)");

The code produces output similar to the following:

You are 53 years, 4 months, and 29 days old. (19508 days total)
Ornelas answered 26/4, 2015 at 16:8 Comment(0)
G
0

If you are looking for an alternative lighter version, have a look at this library that I wrote to use in my own Android app. https://github.com/ashokgelal/samaya

Sorry, I don't have any documentation on its usage, but it is very similar to the counterpart .net Timespan class. Also, there are some unit tests which contains many examples on how to use it.

Grattan answered 10/8, 2011 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.