• Register
1 vote
40 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

15.3k points

Please log in or register to answer this question.

1 Answer

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

I hope this will help you.

94.3k points