Introduction

This example shows you how to convert milliseconds into years, months, weeks, days, hours, minutes, seconds in Java. Sometimes we need to convert the milliseconds into years or months or weeks or days or hours or minutes or seconds.

For example, when you want to check the exact time taken by a program to execute then you may need to calculate the time. So in this case you get the start time in milliseconds and end time in milliseconds and you want to check how much time that program took to execute. Therefore you would like to convert the milliseconds into minutes and seconds to make it more readable format because millisecond unit may not be so understandable quickly.

You may also like to read Convert Milliseconds into Days, Hours, Minutes, Seconds in Java.

Prerequisites

Java 1.8

Example with Source Code

The following Java source code will give you the exact thing what you are looking for.

We are not using any modulo (%) or division (/) operators to convert milliseconds into years, months, weeks, days, hours, minutes, seconds but using Java’s built-in functions to make it happen.

In the below source code we first find out the number of years, then months, then weeks, then days, then hours, then minutes and so on. If we do not find any integer value for any unit of time then it becomes 0.

package com.jeejava.time;

import java.util.concurrent.TimeUnit;

public class MillisToYearMonthDayHrMinSec {

	public static void main(String[] args) {
		final long milliseconds = 547896541576l;
		long dy = TimeUnit.MILLISECONDS.toDays(milliseconds);
		final long yr = dy / 365;
		dy %= 365;
		final long mn = dy / 30;
		dy %= 30;
		final long wk = dy / 7;
		dy %= 7;
		final long hr = TimeUnit.MILLISECONDS.toHours(milliseconds)
				- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(milliseconds));
		final long min = TimeUnit.MILLISECONDS.toMinutes(milliseconds)
				- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliseconds));
		final long sec = TimeUnit.MILLISECONDS.toSeconds(milliseconds)
				- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds));
		final long ms = TimeUnit.MILLISECONDS.toMillis(milliseconds)
				- TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(milliseconds));
		System.out.println(
				String.format("%d Years %d Months %d Weeks %d Days %d Hours %d Minutes %d Seconds %d Milliseconds", yr,
						mn, wk, dy, hr, min, sec, ms));
	}

}

Testing the Program

When you run the above program you will see below output in the console:

Output

17 Years 4 Months 2 Weeks 2 Days 9 Hours 29 Minutes 1 Seconds 576 Milliseconds

That’s all. Hope you got idea how to convert milliseconds into years, months, weeks, days, hours, minutes, seconds. If you want you may also calculate other units like months, years etc.

You may also like to read Convert Milliseconds into Days, Hours, Minutes, Seconds in Java.

Source Code

You can download source code.

Thanks for reading.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *