0

The title of the question includes all the details.

If I have BigDecimal seconds = new BigDecimal("32365423.56");
Is there API methods that convert this to:
n years, n months, n days, n hours, n minutes, n seconds.

If time is ambiguous in seconds, then assume in seconds these values: (I'm not confined to these values)

   BigDecimal year = new BigDecimal("31556908.8");
   BigDecimal month = new BigDecimal("2629739.52");
   BigDecimal day = new BigDecimal("86400");
   BigDecimal hour = new BigDecimal("3600");
   BigDecimal minute = new BigDecimal("60");
Saleh Feek
  • 2,048
  • 8
  • 34
  • 56
  • 1
    Unlikely. The question is ambiguous, since a month does not have a fixed size. A precise answer would depend not just on the number of seconds, but when the interval begins (or equivalently, when it ends). – chepner May 23 '14 at 17:34
  • @chepner I add some detail to the question, I don't know if that can facilitate. Any way thank you. – Saleh Feek May 23 '14 at 17:45

2 Answers2

1

You can use java.time.Duration which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation. With Java-9 some more convenient methods were introduced.

Demo:

import java.math.BigDecimal;
import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        BigDecimal seconds = new BigDecimal("32365423.56");
        Duration duration = Duration.ofNanos(seconds.multiply(BigDecimal.valueOf(1_000_000_000)).longValue());
        System.out.println(duration);

        // Custom format
        // ####################################Java-8####################################
        String formattedDuration = String.format("%d days %d hours %d minutes %d seconds", duration.toDays(),
                duration.toHours() % 24, duration.toMinutes() % 60, duration.toSeconds() % 60);
        System.out.println(formattedDuration);
        // ##############################################################################

        // ####################################Java-9####################################
        formattedDuration = String.format("%d days %d hours %d minutes %d seconds", duration.toDaysPart(),
                duration.toHoursPart(), duration.toMinutesPart(), duration.toSecondsPart());
        System.out.println(formattedDuration);
        // ##############################################################################
    }
}

Output:

PT8990H23M43.56S
374 days 14 hours 23 minutes 43 seconds
374 days 14 hours 23 minutes 43 seconds

Learn about the modern date-time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

You could use the java.util.Date class http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#Date%28long%29 together with the java.util.Calendar class http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html

You would is it like that:

long l = Math.round(seconds.doubleValue());
Date d = new Date(l);
Calendar c = new GregorianCalendar();
c.setTime(d);
int sec = c.get(Calendar.SECOND);
int min = c.get(Calendar.MINUTE);
...
wastl
  • 2,643
  • 14
  • 27