Best way to convert months into Milliseconds

I'm trying to convert a no of months into milliseconds

For example: 6 months = X milliseconds

Jon Skeet
people
quotationmark

There's no fixed answer to that, because it depends on which months those are - and indeed which year it is. Also potentially which time zone you're in, if you want to take account of that. (I'm assuming you mean the Gregorian calendar, by the way - different calendar systems have different month lengths.)

You could get some sort of "reasonable approximation" by assuming 365.25 days in a year, and saying that 6 months is half of that, then find out that many days in milliseconds. But it would only be an approximation.

For "how many milliseconds does it take to get from date/time X to 6 months later" you'd use an API (even Calendar would work for this particular case, although I'd recommend Joda Time or java.time in Java 8):

  • Set your start date/time, in the appropriate calendar and time zone
  • Fetch the "milliseconds since the Unix epoch" (which is easy enough to retrieve in any API) and remember it
  • Add 6 months
  • Fetch the "milliseconds since the Unix epoch" again, and subtract the earlier value from it

people

See more on this question at Stackoverflow