10

I'm trying to convert a Milliseconds date to number of years months weeks and days.

For example: 5 months, 2 weeks and 3 days or 1 year and 1 day.

I don't want: 7 days or 4 weeks > this should be 1 week and 1 month.

I tried few ways but it always became something like 7 days and 0 weeks.

My code:

int weeks = (int) Math.abs(timeInMillis / (24 * 60 * 60 * 1000 * 7));
int days = (int) timeInMillis / (24 * 60 * 60 * 1000)+1);

I have to add 1 to number of days because if I have 23 hours it should be 1 day.

Please explain how to convert it correctly, I think that there is more efficient ways to do it.

Hartok
  • 2,147
  • 20
  • 37
Dennis
  • 2,271
  • 6
  • 26
  • 42

3 Answers3

36

I always use this for getting years etc from milliseconds and vice versa. Till now I've had no problems with it. Hope it helps.

import java.util.Calendar;

Calendar c = Calendar.getInstance(); 
//Set time in milliseconds
c.setTimeInMillis(milliseconds);
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH); 
int mDay = c.get(Calendar.DAY_OF_MONTH);
int hr = c.get(Calendar.HOUR);
int min = c.get(Calendar.MINUTE);
int sec = c.get(Calendar.SECOND);
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • I want to know how many days,weeks,etc. in it, not the date itself. – Dennis Mar 17 '13 at 14:00
  • 1
    The milliseconds are calculated from 1 Jan 1970. So, you have final date and initial date. Making that logic to get days, months etc between two dates should not be hard. – Shobhit Puri Mar 17 '13 at 14:06
  • I think it works. `int mYear = c.get(Calendar.YEAR)-1970;` `int mMonth = c.get(Calendar.MONTH);` `int mDay = c.get(Calendar.DAY_OF_MONTH)-1;` Tell me please if this is right. – Dennis Mar 17 '13 at 14:24
  • It seems fine. I guess their should be no need of subtracting 1 day from the current days because that 1st Jan 1970 is also counted in the milliseconds. – Shobhit Puri Mar 17 '13 at 14:28
  • Yes, but then if my time in millis is 86400000 (= 1 day) I get `2 days`. Do you know why? – Dennis Mar 17 '13 at 14:34
9

Thanks to Shobhit Puri my problem was solved.

This code calculates how many months, days etc. are in a given time in milliseconds. I use it to calculate difference between two dates.

Full solution:

long day = (1000 * 60 * 60 * 24); // 24 hours in milliseconds
long time = day * 39; // for example, 39 days

Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
int mYear = c.get(Calendar.YEAR)-1970;
int mMonth = c.get(Calendar.MONTH); 
int mDay = c.get(Calendar.DAY_OF_MONTH)-1;
int mWeek = (c.get(Calendar.DAY_OF_MONTH)-1)/7; // ** if you use this, change the mDay to (c.get(Calendar.DAY_OF_MONTH)-1)%7

Thank you again!

Community
  • 1
  • 1
Dennis
  • 2,271
  • 6
  • 26
  • 42
  • Calendar c = Calendar.getInstance(); //Set time in milliseconds c.setTimeInMillis(milliseconds); int mYear = c.get(Calendar.YEAR); int mMonth = c.get(Calendar.MONTH); int mDay = c.get(Calendar.DAY_OF_MONTH); –  Jul 23 '14 at 15:46
  • Can you please explain how it works ie. how it returns the number of months? – Madhan Sep 30 '21 at 16:36
4

Source:Convert time interval given in seconds into more human readable form

function secondsToString(seconds)
{
var numyears = Math.floor(seconds / 31536000);
var numdays = Math.floor((seconds % 31536000) / 86400); 
var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
return numyears + " years " +  numdays + " days " + numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";

}
Community
  • 1
  • 1
Karan_Rana
  • 2,813
  • 2
  • 26
  • 35