# Which operation could we perform in order to find the number of milliseconds in a year?

557 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.

## Solution :

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

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;`

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.

## Related questions

1 answer 28 views
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."); } }}
1 vote
1 answer 28 views
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
1 answer 11 views
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
1 answer 31 views
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?
1 answer 1 view
1 view
problem: i am in a trouble excel convert date to number of days .
1 answer 30 views
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?
1 answer 14 views
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
1 answer 28 views
28 views
I need to know how to disable previous date in bootstrap datepicker.
1 vote
1 answer 7 views
7 views
Problem: I&rsquo;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?