-6

I'm working on a java program that calculates a given amount of seconds to the amount of years, days, hours, all the way to plancks. I can enter in the google search bar "31 years, 259 days, 5 hours, 54 minutes" and i'll get a result stating "31.7097916 years." How does it calculate this? I'd like to implement this into my program if possible.

tbodt
  • 16,609
  • 6
  • 58
  • 83
  • 2
    http://www.joda.org/joda-time/ – Robert Harvey May 29 '14 at 00:27
  • If there a method you've tried that's not giving you good results? Google isn't likely to come and share their code, and anything else is guessing or just telling you how we'd do it. And what @Robert linked above is excellent! (Btw; is that actually the answer?) – Andrew Barber May 29 '14 at 00:30
  • I think it's really misleading of Google to give an answer like this. Exactly what proportion of a year "259 days, 5 hours, 54 minutes" amounts to depends on which year it is, and also whether daylight savings starts or ends during the 259 days. There's actually no way to calculate this correctly. Even with the mighty Joda. – Dawood ibn Kareem May 29 '14 at 02:32

1 Answers1

3

Just do the math, for 31 years, 259 days, 5 hours, 54 minutes

float total = 31f;

// Days to years
total = 259 / 365;

// Hours to years
total = total + ((5 / 24) / 365);

// Minutes to years
total = total + (((54 / 60) / 24) / 365);

System.out.println("Total = " + total);

As you can see, there is a logic to this. For a more complex (but still very simple) implementation

public float daysToYear(int days){
    return days / 365;
}

public float hoursToYear(int hours){
    return (hours / 24) / 365;
}

public float minsToYear(int mins){
    return ((min / 60) / 24) / 365;
}

public float secsToYear(int secs){
    return (((secs / 60) / 60) / 24) / 365;
}

public float millisToYear(int millis){
    return ((((millis / 1000) / 60) / 60) / 24) / 365;
}

As you can see, these conversions themselves can be simplified and modified to fit anything you would need; for example you could simplify millisToYear() to

public float millisToYear(double millis){
    return secsToYear(millis/1000);
}

Notice the change of parameter type to double from int. Of course, as mentioned in the comments below, if you want more complexity implemented (time zones, leap year, etc.) then you need to take those into consideration. Again, simple logic. If you need help comment and I will improve my answer, if you are super nice about it :P

zgc7009
  • 3,371
  • 5
  • 22
  • 34
  • 4
    You seem to be missing the 31 years part, and also a year doesn't have 356 days in it... – azurefrog May 29 '14 at 00:34
  • just do the math doesn't consider leap years etc etc etc – Matt Coubrough May 29 '14 at 00:35
  • Haha it has been a super long day – zgc7009 May 29 '14 at 00:35
  • 1
    It may have been a long day, but at least it was a super short year! – Matt Coubrough May 29 '14 at 00:36
  • 2
    Don't forget to account for leap years and [time zone discontinuities](http://stackoverflow.com/a/6841479/102937). – Robert Harvey May 29 '14 at 00:36
  • @Robert Harvey correct, I will add an edit. This is just a super simple example how to get started at what he wants (since he didn't even have code pasted). Lesson learned on copy and paste too, make sure what you are copying is right. – zgc7009 May 29 '14 at 00:37
  • 3
    Kinda why I mentioned Joda Time. People far smarter than us have figured out this crap already. – Robert Harvey May 29 '14 at 00:38
  • @Robert Harvey sure, he was just asking how to do it, and that is why I was showing him a VERY basic example of what it looks like. – zgc7009 May 29 '14 at 00:39
  • 1
    Exactly! always prefer a well-tested library than roll-your-own code. Sadly, there is no "super simple example" of roll your own time calculations. I blame the orbit of the earth! – Matt Coubrough May 29 '14 at 00:40