# How many milliseconds are in a year

1 vote
23 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 vote

## Solution:

You can easily find out the total milliseconds in a calendar year by multiplying them sequentially.

Let's see how we can do it:

private static final int MILLIS_IN_SECOND = 1000;
private static final int SECONDS_IN_MINUTE = 60;
private static final int MINUTES_IN_HOUR = 60;
private static final int HOURS_IN_DAY = 24;
private static final int DAYS_IN_YEAR = 365;

private static final long MILLISECONDS_IN_YEAR = (long) MILLIS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * HOURS_IN_DAY * DAYS_IN_YEAR;

You can also find out the millis by built-in the Calendar function in java. Right, this way:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
int daysInYear = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);

System.out.println(TimeUnit.DAYS.toMillis(daysInYear));

## Related questions

178 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 = ... 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.
16 views
Problem: I just started learning java and solving problems from various online websites. Stuck on a problem saying, count how many times a substring appears in a string java. This problem is beyond my understanding. I found Kodlogs very helpful and friendly environment ... possible solution (with code snippet) would be great for me, and I must recognize your help. Thanks in advance, mate.