• Register
0 votes

Problem :

I want to convert the Milliseconds in the format of number of years months weeks and days.

e.g. 10 months, 4 weeks and 5 days

I have already tried in few ways but it is becoming as 7 days and 0 weeks.

Please find below my code for your referance:

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

int days = (int) timeInMillis / (24 * 60 * 60 * 1000)+1);

I am trying the logic by adding 1 to number of days as if I have 23 hours it should be 1 day.

Please give me the solution on it.

6 5 3
7,540 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

I had also faced this problem in the past. I had done lot of research on it and found the solution.

Please follow the below approach to resolve your issue.

I tend to use following code to get the years and other dates from the milliseconds and vice versa. Till now my below logic never failed . So I hope it helps to you too.

import java.util.Calendar;
long myday = (1000 * 60 * 60 * 24); //converting 24 hours in milliseconds
long mytime = myday * 40; //e.g. 40 days
Calendar mycal = Calendar.getInstance();
int myYear = mycal.get(Calendar.YEAR)-1970;
int myMonth = mycal.get(Calendar.MONTH); 
int myDay = mycal.get(Calendar.DAY_OF_MONTH)-1)%7;
int myWeek = (mycal.get(Calendar.DAY_OF_MONTH)-1)/7;
9 7 4
38,600 points
0 votes

Sometimes it is mandatory to reveal time in milliseconds as another alternative for time function. There is a good equation by which you can convert your time into milliseconds. The year may normal or leap year we assume normal year for this article and we will make an equation or formula to insert our system for time calculation in milliseconds.


In many websites and other systems which require time function we usually use second unit but it is more accurate and easy to use milliseconds instead of second. Basically it is used in many computer languages for more readability and functional of the time constraints.

Let’s use days, hours, minutes, seconds, and miliseconds

Now if a normal year has 365 days and,
1 day has 24 hours

1 hour has 60 minutes

1 minute has 60 seconds and

1 second has 1000 milliseconds

Then the number of milliseconds in a year can be calculated in this way;

Days multiply by hours multiply by minutes multiply by seconds multiply by milliseconds will give you the number of milliseconds in a year.

365 days x 24 hours x 60 minutes x 60 seconds x 1000 milliseconds


This is a big number of billions milliseconds.


So it becomes a good technique to calculate total milliseconds of a year in very short by using simple math calculation and formula. 

10 6 4
31,120 points

Related questions

0 votes
1 answer 28 views
Problem: I am struggling to show the next leap year. I have my initial leap year. What and where should I code? Conclusion: 2016 is a leap year. 2020 is a leap year following 2016. import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner ... .println(inputYear + " is a leap year."); } else { System.out.println(inputYear + " is not a leap year."); } }}
asked Dec 20, 2020 sasha 16.2k points
1 vote
1 answer 28 views
Problem: Hi fellas, I am a very new student in java programming. I am just wondering if I can find milliseconds in a year by a java program. Do you have any idea how to write such a program? Thanks
asked Mar 10, 2020 Gavin 15.3k points
0 votes
1 answer 11 views
Problem: I want to get month and year from date in SQL SERVER. I do not know how to do that. If someone could help me, it would be appreciated. Thanks
asked Oct 19, 2020 Code Learner 9.2k points
0 votes
1 answer 31 views
Get month and year from the date in SQL I need to access the only month.year from the date field in SQL Server. Any idea?
asked Dec 5, 2020 miki 1.9k points
0 votes
1 answer 1 view
1 view
problem: i am in a trouble excel convert date to number of days .
asked 11 hours ago ashik 10.6k points
0 votes
1 answer 30 views
Problem: How can I get the name of the month from the month number? For example, if I have 3, I want to returnmarch date.tm_month() How do I get the string march?
asked Dec 1, 2020 sasha 16.2k points
0 votes
1 answer 14 views
Problem: select GETDATE() Returns: 2008-09-22 15:24:13.790 I want the date part, without the time part:2008-09-22 00:00:00.000
asked Feb 15 sasha 16.2k points
0 votes
1 answer 28 views
I need to know how to disable previous date in bootstrap datepicker.
asked Oct 4, 2020 Daniel Anderson 4k points
1 vote
1 answer 7 views
Problem: I’m new in data frame. Here is an example of my data set; Date Time(GMT)Depth Temp Salinity Density Phosphate 24/06/2002 1000 1 33.855 0.01 24/06/2002 1000 45 33.827 0.01 01/07/2002 1000 10 13.26 33.104 24.873 0.06 01/07/2002 1000 30 12.01 ... only have one row per date, I want to do this based on the Depth, I would like to keep the row with the greatest (deepest) depth. Any ideas?
asked Mar 10 Shovo210 13.2k points
0 votes
1 answer 3 views
Problem: I am stuck with this ... Please help me that how to deal with this? Java get current date without time
asked Mar 25 Ifra 24.4k points