-2

I'm struggling to find a solution as to converting seconds into years, months, weeks, hours, minutes, seconds.

Example:

public Time(int inputSeconds) {
    int years = 0;
    int months = 0;
    int weeks = 0;
    int days = 0;
    int hours = 0;
    int seconds = 0;
}
Faris Rehman
  • 129
  • 1
  • 4
  • 11
  • Try googling seconds to hours...seconds to days...seconds to weeks...etc... – brso05 Apr 16 '15 at 16:57
  • 8
    You cannot convert seconds into months. A month is not a standard amount of time – ControlAltDel Apr 16 '15 at 16:58
  • How many seconds in a month? – Boris the Spider Apr 16 '15 at 16:59
  • 2
    Some months have leap seconds. Some months have 28, 29, 30,or 31 days. Some years have 365 or 366 days. One year had 11 less days or 2 more days depending on where you lived. – Peter Lawrey Apr 16 '15 at 17:02
  • the best you can do is compute from an epoch. I am using 1.1.2000 00:00:00 as epoch start.. So any time in seconds will be added to this epoch... then process the remainders so minutes are mod 60, hours are mod 60 of what is left then days mod 24. The month is much much trickier you need to have table of how many days per month and iterate through it until the added time is 0 ... It is not that easy to code (mine source code in C++ for this for astronomy apps is about 5.3KB long)... some IDEs have function for this ... I think PHP had some function for this and WinAPI too – Spektre Apr 16 '15 at 17:21
  • 1
    and that is not accounting the iregularities @PeterLawrey mentioned there were quite a lot of them in the past .... google Julian Date/Callendar it may be of help – Spektre Apr 16 '15 at 17:23
  • Thanks, yeah, @kavi suggested the Date class anyway. – Faris Rehman Apr 16 '15 at 17:29

2 Answers2

3

First of all my advice is to change time variable type from int to long.

There are certain method in Date class which will help you to achieve this but remember as of now these methods are deprecated.

import java.util.Date;
import java.util.Random;


class MyTime {

    int years;
    int months;
    int day;
    int hours;
    int seconds;

    public MyTime(long inputSeconds) {
        Date d = new Date(inputSeconds);
        this.years = d.getYear();
        this.months = d.getMonth();
        this.day = d.getDay();
        this.hours = d.getHours();
        this.seconds = d.getSeconds();
    }

    public static void main(String[] args) {
        new MyTime(new Date().getTime()).show();
    }

    public void show() {
        System.out.println("" + years + "");
        System.out.println("" + months + "");
        System.out.println("" + day + "");
        System.out.println("" + hours + "");
        System.out.println("" + seconds + "");
    }

}
Marius Schär
  • 336
  • 2
  • 17
kavi temre
  • 1,321
  • 2
  • 14
  • 21
2

For weeks, days, hours, minutes and seconds it is possible doing using some simple maths in a conversion such as this:

int weeks = seconds / 604800;
int days = (seconds % 604800) / 86400;
int hours = ((seconds % 604800) % 86400) / 3600;
int minutes = (((seconds % 604800) % 86400) % 3600) / 60;
seconds = (((seconds % 604800) % 86400) % 3600) % 60;

The syntax is just for you to understand where the previous value come from.

For Years and Months it depends on the month (for example February has less days than any other) and if you are taking into account a leap year (366 days) or not.

Alfie
  • 69
  • 1
  • 10
Rui Esteves
  • 130
  • 9
  • Thanks, and yeah, I just figured out that this won't even be good as there's sometimes different days, etc. Thanks again. – Faris Rehman Apr 16 '15 at 17:22