• Register
0 votes
281 views

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();
mycal.setTimeInMillis(time);
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.

Reason:  

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

365x24x60x60x1000=31,536,000,000

This is a big number of billions milliseconds.

Solution:

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 11 views
11 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 5.3k points
0 votes
1 answer 6 views
6 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 5.2k points
1 vote
1 answer 27 views
27 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 18 views
18 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 30 views
0 votes
1 answer 10 views
10 views
I need to know how to disable previous date in bootstrap datepicker.
asked Oct 4, 2020 Daniel Anderson 4k points
2 votes
1 answer 12 views
12 views
Problem: Hello all, I am a secondary school level student and recently started learning Java programming out my curiosity. I have got a textbook on Java programming. I just completed the chapter of for-loop and continue solving the exercises of this chapter. I have got weird a ... you guys. But, yeah it is important to know for me. Thanks for being nice to me. I must recognize your help. Thanks.
asked Apr 12, 2020 Gavin 15.3k points
0 votes
1 answer 17 views
17 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 5.3k points
0 votes
1 answer 21 views
21 views
I was working on that but I got into some problems.
asked Nov 3, 2020 TeamScript 9.2k points