You can use Duration for this:

Duration.of(5, ChronoUnit.SECONDS).toMillis()
Answer from Andrew Rueckert on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › time › temporal › TemporalUnit.html
TemporalUnit (Java Platform SE 8 )
October 20, 2025 - // these two lines are equivalent, but the second approach is recommended temporal = thisUnit.addTo(temporal); temporal = temporal.plus(thisUnit); It is recommended to use the second approach, plus(TemporalUnit), as it is a lot clearer to read in code.
🌐
JBoss
docs.jboss.org › hibernate › orm › 6.0 › javadocs › org › hibernate › query › sqm › TemporalUnit.html
TemporalUnit (Hibernate JavaDocs)
Usually the smallest unit of fractional seconds, either milliseconds or microseconds. We define this value in order to avoid repeatedly converting between NANOSECONDs and a unit that the database understands. On some platforms this is also used to avoid numeric overflow.
Top answer
1 of 1
4

I am not sure if you speak about the new java.time-package of Java-8 or about Joda-Time so I try to present solutions for both libraries.

Most important aspect however is that you cannot divide a Duration defined in seconds into years, months etc. in a self-consistent manner because month-based units vary in length of seconds and days. At least not possible without any trick.

Best you can do in this case is to use a reference timestamp in order to recalculate the duration you have. That means you add your duration to the reference timestamp and then evaluate the new duration between the old reference timestamp and the result in years, months, days etc. This is also called normalization and has nothing to do with printing/formatting.

Java-8:

Duration dur = Duration.ofSeconds(5000001); // example
LocalDateTime ref = LocalDateTime.now(); // reference timestamp
LocalDateTime end = ref.plus(dur);

System.out.println(ref);
System.out.println(end);

// normalize first the calendrical part
LocalDateTime ldt = ref;
long years = ChronoUnit.YEARS.between(ldt, end);

// find the months part
ldt = ldt.plus(years, ChronoUnit.YEARS);
long months = ChronoUnit.MONTHS.between(ldt, end);

// find the days part
ldt = ldt.plus(months, ChronoUnit.MONTHS);
long days = ChronoUnit.DAYS.between(ldt, end);

// find the hours part
ldt = ldt.plus(days, ChronoUnit.DAYS);
long hours = ChronoUnit.HOURS.between(ldt, end);

// find the minutes part
ldt = ldt.plus(hours, ChronoUnit.HOURS);
long minutes = ChronoUnit.MINUTES.between(ldt, end);

// find the seconds part
ldt = ldt.plus(minutes, ChronoUnit.MINUTES);
long seconds = ChronoUnit.SECONDS.between(ldt, end);

// print the new normalized duration in ISO-8601-format
System.out.println(
  String.format("P%1dM%3dH%5dS", years, months, days, hours, minutes, seconds));

// example output
// 2015-03-17T12:54:07.943
// 2015-05-14T09:47:28.943
// P0Y1M26DT20H53M21S

Compared with old JDK pre 8 this can be considered as much better because at least elementary methods for calculation of a duration in one given unit are offered. But a general duration type for handling all units spanning from years to seconds is completely missing. And the best duration formatter I could find is just java.util.Formatter.

Joda-Time

That is the second-best Java library when duration handling is needed, in most details better than Java-8 on this area. Joda-Time indeed offers a duration type spanning from years to seconds (and millis) called Period. See here the much simpler solution:

Duration dur = new Duration(5000001 * 1000L); // in milliseconds
LocalDateTime ref = new LocalDateTime(); // reference timestamp
LocalDateTime end = ref.plus(dur);

// construct normalized duration
PeriodType type = PeriodType.yearMonthDayTime().withMillisRemoved();
Period p = new Period(ref, end, type);

// print the new normalized duration
System.out.println(p); // P1M26DT20H53M21S

Small note: I have left out fractional seconds (in Joda-Time limited to milliseconds, in Java-8 up to nanoseconds) in given examples. It is easy to enhance the examples if you really have need for this precision.

🌐
Java2s
java2s.com › example › java-api › java › time › temporal › chronounit › millis-0.html
Example usage for java.time.temporal ChronoUnit MILLIS
public static ChronoUnit convert(TimeUnit tu) { if (tu == null) { return null; }//from ww w.j a va 2 s. c o m switch (tu) { case DAYS: return ChronoUnit.DAYS; case HOURS: return ChronoUnit.HOURS; case MINUTES: return ChronoUnit.MINUTES; case SECONDS: return ChronoUnit.SECONDS; case MICROSECONDS: return ChronoUnit.MICROS; case MILLISECONDS: return ChronoUnit.MILLIS; case NANOSECONDS: return ChronoUnit.NANOS; default: assert false : "there are no other TimeUnit ordinal values"; return null; } }
🌐
Dariawan
dariawan.com › tutorials › java › java-time-duration-tutorial-examples
java.time.Duration Tutorial with Examples | Dariawan
August 29, 2019 - static Duration of(long amount, ... number of standard hours. static Duration ofMillis(long millis): Obtains a Duration representing a number of milliseconds....
🌐
Tabnine
tabnine.com › home page › code › java › java.time.temporal.temporalunit
java.time.temporal.TemporalUnit java code examples | Tabnine
private static long getNext(long time, TemporalUnit unit, long multiplier, ZoneId zoneId) { final long end; try { if (!unit.isDateBased()) end = time + unit.getDuration().multipliedBy(multiplier).toMillis(); else end = ZonedDateTime.ofInstant(Instant.ofEpochMilli(time), zoneId).plus(multiplier, unit).toInstant().toEpochMilli(); } catch (ArithmeticException e) { return multiplier > 0 ?
Find elsewhere
🌐
Program Creek
programcreek.com › java-api-examples
java.time.temporal.TemporalUnit Java Examples
January 20, 2026 - @Test public void test_isSupported_TemporalUnit() { assertEquals(TEST_2008.isSupported((TemporalUnit) null), false); assertEquals(TEST_2008.isSupported(ChronoUnit.NANOS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.MICROS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.MILLIS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.SECONDS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.MINUTES), false); assertEquals(TEST_2008.isSupported(ChronoUnit.HOURS), false); assertEquals(TEST_2008.isSupported(ChronoUnit.HALF_DAYS), false); assertEquals(TEST_2008.isSupported(Chro
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › time › Duration.html
Duration (Java Platform SE 8 )
1 week ago - millis - the number of milliseconds, positive or negative · Returns: a Duration, not null · public static Duration ofNanos(long nanos) Obtains a Duration representing a number of nanoseconds. The seconds and nanoseconds are extracted from the specified nanoseconds.
🌐
Javastudyguide
ocpj8.javastudyguide.com › ch21.html
Java 8 Programmer II Study Guide: Exam 1Z0-809
August 25, 2015 - Notice that the method get(TemporalUnit) only supports SECONDS and NANOS. Once an instance of Duration is created, we cannot modify it, but we can create another instance from an existing one. One way is to use the with method and its versions: Duration duration1sec8nan = oneSecond.withNanos(8); Duration duration2sec1nan = oneSecond.withSeconds(2).withNanos(1); Another way is by adding or subtracting days, hours, minutes, seconds, milliseconds, or nanoseconds: // Adding Duration plus4Days = oneSecond.plusDays(4); Duration plus3Hours = oneSecond.plusHours(3); Duration plus3Minutes = oneSecond.p
🌐
Javadoc.io
javadoc.io › doc › com.github.signaflo › timeseries › 0.4 › com › github › signaflo › timeseries › TimeUnit.html
TimeUnit - timeseries 0.4 javadoc
Bookmarks · Latest version of com.github.signaflo:timeseries · https://javadoc.io/doc/com.github.signaflo/timeseries · Current version 0.4 · https://javadoc.io/doc/com.github.signaflo/timeseries/0.4 · package-list path (used for javadoc generation -link option) · https://javadoc.io/d...
🌐
Java Tips
javatips.net › api › java.time.temporal.temporalunit
Java Examples for java.time.temporal.TemporalUnit
public static long toUnit(TemporalUnit unit, Duration duration) { switch((ChronoUnit) unit) { case NANOS: return duration.toNanos(); case MICROS: return toMicros(duration); case MILLIS: return duration.toMillis(); case SECONDS: return duration.getSeconds(); } if (unit.getDuration().getNano() == 0) { return duration.getSeconds() / unit.getDuration().getSeconds(); } throw new IllegalArgumentException("Unsupported sub-second unit " + unit); }
🌐
Android Developers
developer.android.com › api reference › temporalunit
TemporalUnit | API reference | Android Developers
February 13, 2023 - Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Top answer
1 of 2
3

You could model your class after a well-known and tested class: java.time.LocalDate, which provides the plus(long, TemporalUnit) method.

Similarly, you could create a someMethod(long, TimeUnit) method that allows callers to pass in arbitrary amounts of any TimeUnit.

void someMethod(long amount, TimeUnit unit) {
    this.longValue = unit.toMillis(amount);
}

Note that LocalDate also provides specialized methods for adding certain common units of time, like plusDays(). That gives the caller the ability to decide which is clearer for the code they're writing:

LocalDate tomorrow = today.plusDays(1);
LocalDate tomorrow = today.plus(1, TimeUnit.DAYS);
2 of 2
2

It seems to me that you don’t need to develop your own class; that you’ll be reinventing the wheel. I suggest you use the Duration class.

To convert from some time unit to a Duration:

    System.out.println(Duration.of(3, ChronoUnit.HOURS));

Or alternatively:

    System.out.println(Duration.ofHours(3));

Output is the same in both cases:

PT3H

It prints a little funny; this means a span of time of 3 hours. The format is ISO 8601.

    System.out.println(Duration.of(2, ChronoUnit.DAYS));

PT48H

48 hours; that’s correct.

    System.out.println(Duration.of(327864523, ChronoUnit.MICROS));

PT5M27.864523S

A span of 5 minutes 27.864523 seconds.

If you need to convert to milliseconds, the method is built in:

    Duration dur = Duration.of(284, ChronoUnit.MINUTES);
    System.out.println("" + dur.toMillis() + " milliseconds");

17040000 milliseconds

Duration is part of java.time, the modern Java date and time API, and of course works well with the other classes from that API.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Documentation: the Duration class
  • Wikipedia article: ISO 8601; section on durations
🌐
Baeldung
baeldung.com › home › java › java dates › time conversions using timeunit
Time Conversions Using TimeUnit | Baeldung
November 16, 2023 - In the example, we’ve just converted 3672 milliseconds to the finest unit representation, which includes hours, minutes, and seconds.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Temporal › Duration › milliseconds
Temporal.Duration.prototype.milliseconds - JavaScript | MDN
The milliseconds accessor property of Temporal.Duration instances returns an integer representing the number of milliseconds in the duration.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.concurrent.timeunit
TimeUnit Class (Java.Util.Concurrent) | Microsoft Learn
A nanosecond is defined as one thousandth of a microsecond, a microsecond as one thousandth of a millisecond, a millisecond as one thousandth of a second, a minute as sixty seconds, an hour as sixty minutes, and a day as twenty four hours.