Monday, 15 April 2024

Utility class to convert the time in double to different time formats like days, hours, minutes, seconds etc.,

Time is a fundamental aspect of programming, and in Java, it's essential to understand the different units of time and how they relate to each other. Whether you're working with nanoseconds, milliseconds, seconds, or days, being able to convert between these units is crucial for various applications. In this blog post, we'll delve into the intricacies of time dimensions and provide utility classes to convert between them seamlessly.

 

The Basics: Time Units

Before we dive into conversions, let's establish the basic units of time we'll be working with:

 

1.   Nanoseconds (ns): The smallest unit of time commonly used in programming. It represents one billionth of a second.

2.   Microseconds (μs): One thousand nanoseconds make up a microsecond.

3.   Milliseconds (ms): A millisecond is equal to one thousand microseconds or one million nanoseconds.

4.   Seconds (s): A standard unit of time, where one second equals one thousand milliseconds.

5.   Minutes (min): Sixty seconds make up a minute.

6.   Hours (hr): An hour consists of sixty minutes.

7.   Days (d): A day comprises twenty-four hours.

 

Now that we have a grasp of these time dimensions, let's explore how we can convert between them efficiently.

 

Example: Calculate number of seconds per day.

To calculate the number of seconds in one day, you need to consider that there are 24 hours in a day, 60 minutes in an hour, and 60 seconds in a minute. So, you can use the following formula:

 

seconds = days × 24 × 60 × 60

seconds=days×24×60×60

 

Let's compute it for one day

 

seconds = 1 × 24 × 60 × 60 = 86,400

 

So, there are 86,400 seconds in one day.

 

 

Utility Class for Time Conversion

To facilitate time conversions in Java, we can create a utility class that encapsulates the conversion logic. Below is a Java class TimeConverter that provides static methods for converting between different time units.

 

TimeConverter.java

package com.sample.app.util;

import java.util.concurrent.TimeUnit;

public final class TimeConverter {

	private static final long ONE_NANO_SECOND = 1L;

	private static final long ONE_MICRO_SECOND_TO_NANO = ONE_NANO_SECOND * 1000L;

	private static final long ONE_MILLI_SECOND_TO_NANO = ONE_MICRO_SECOND_TO_NANO * 1000L;

	private static final long ONE_SECOND_TO_NANO = ONE_MILLI_SECOND_TO_NANO * 1000L;

	private static final long ONE_MINUTE_TO_NANO = ONE_SECOND_TO_NANO * 60L;

	private static final long ONE_HOUR_TO_NANO = ONE_MINUTE_TO_NANO * 60L;

	private static final long ONE_DAY_TO_NANO = ONE_HOUR_TO_NANO * 24L;

	private TimeConverter() {
	}

	public static double convert(double t, TimeUnit sourceUnit, TimeUnit destinationUnit) {
		switch (sourceUnit) {
		case NANOSECONDS:
			return nanosToUnit(t, destinationUnit);
		case MICROSECONDS:
			return microsToUnit(t, destinationUnit);
		case MILLISECONDS:
			return millisToUnit(t, destinationUnit);
		case SECONDS:
			return secondsToUnit(t, destinationUnit);
		case MINUTES:
			return minutesToUnit(t, destinationUnit);
		case HOURS:
			return hoursToUnit(t, destinationUnit);
		case DAYS:
		default:
			return daysToUnit(t, destinationUnit);
		}
	}

	public static double nanosToUnit(double nanos, TimeUnit destinationUnit) {
		switch (destinationUnit) {
		case NANOSECONDS:
		default:
			return nanos;
		case MICROSECONDS:
			return nanos / (ONE_MICRO_SECOND_TO_NANO / ONE_NANO_SECOND);
		case MILLISECONDS:
			return nanos / (ONE_MILLI_SECOND_TO_NANO / ONE_NANO_SECOND);
		case SECONDS:
			return nanos / (ONE_SECOND_TO_NANO / ONE_NANO_SECOND);
		case MINUTES:
			return nanos / (ONE_MINUTE_TO_NANO / ONE_NANO_SECOND);
		case HOURS:
			return nanos / (ONE_HOUR_TO_NANO / ONE_NANO_SECOND);
		case DAYS:
			return nanos / (ONE_DAY_TO_NANO / ONE_NANO_SECOND);
		}
	}

	public static double microsToUnit(double micros, TimeUnit destinationUnit) {
		switch (destinationUnit) {
		case NANOSECONDS:
			return micros * (ONE_MICRO_SECOND_TO_NANO / ONE_NANO_SECOND);
		case MICROSECONDS:
		default:
			return micros;
		case MILLISECONDS:
			return micros / (ONE_MILLI_SECOND_TO_NANO / ONE_MICRO_SECOND_TO_NANO);
		case SECONDS:
			return micros / (ONE_SECOND_TO_NANO / ONE_MICRO_SECOND_TO_NANO);
		case MINUTES:
			return micros / (ONE_MINUTE_TO_NANO / ONE_MICRO_SECOND_TO_NANO);
		case HOURS:
			return micros / (ONE_HOUR_TO_NANO / ONE_MICRO_SECOND_TO_NANO);
		case DAYS:
			return micros / (ONE_DAY_TO_NANO / ONE_MICRO_SECOND_TO_NANO);
		}
	}

	public static double millisToUnit(double millis, TimeUnit destinationUnit) {
		switch (destinationUnit) {
		case NANOSECONDS:
			return millis * (ONE_MILLI_SECOND_TO_NANO / ONE_NANO_SECOND);
		case MICROSECONDS:
			return millis * (ONE_MILLI_SECOND_TO_NANO / ONE_MICRO_SECOND_TO_NANO);
		case MILLISECONDS:
		default:
			return millis;
		case SECONDS:
			return millis / (ONE_SECOND_TO_NANO / ONE_MILLI_SECOND_TO_NANO);
		case MINUTES:
			return millis / (ONE_MINUTE_TO_NANO / ONE_MILLI_SECOND_TO_NANO);
		case HOURS:
			return millis / (ONE_HOUR_TO_NANO / ONE_MILLI_SECOND_TO_NANO);
		case DAYS:
			return millis / (ONE_DAY_TO_NANO / ONE_MILLI_SECOND_TO_NANO);
		}
	}

	public static double secondsToUnit(double seconds, TimeUnit destinationUnit) {
		switch (destinationUnit) {
		case NANOSECONDS:
			return seconds * (ONE_SECOND_TO_NANO / ONE_NANO_SECOND);
		case MICROSECONDS:
			return seconds * (ONE_SECOND_TO_NANO / ONE_MICRO_SECOND_TO_NANO);
		case MILLISECONDS:
			return seconds * (ONE_SECOND_TO_NANO / ONE_MILLI_SECOND_TO_NANO);
		case SECONDS:
		default:
			return seconds;
		case MINUTES:
			return seconds / (ONE_MINUTE_TO_NANO / ONE_SECOND_TO_NANO);
		case HOURS:
			return seconds / (ONE_HOUR_TO_NANO / ONE_SECOND_TO_NANO);
		case DAYS:
			return seconds / (ONE_DAY_TO_NANO / ONE_SECOND_TO_NANO);
		}
	}

	public static double minutesToUnit(double minutes, TimeUnit destinationUnit) {
		switch (destinationUnit) {
		case NANOSECONDS:
			return minutes * (ONE_MINUTE_TO_NANO / ONE_NANO_SECOND);
		case MICROSECONDS:
			return minutes * (ONE_MINUTE_TO_NANO / ONE_MICRO_SECOND_TO_NANO);
		case MILLISECONDS:
			return minutes * (ONE_MINUTE_TO_NANO / ONE_MILLI_SECOND_TO_NANO);
		case SECONDS:
			return minutes * (ONE_MINUTE_TO_NANO / ONE_SECOND_TO_NANO);
		case MINUTES:
		default:
			return minutes;
		case HOURS:
			return minutes / (ONE_HOUR_TO_NANO / ONE_MINUTE_TO_NANO);
		case DAYS:
			return minutes / (ONE_DAY_TO_NANO / ONE_MINUTE_TO_NANO);
		}
	}

	public static double hoursToUnit(double hours, TimeUnit destinationUnit) {
		switch (destinationUnit) {
		case NANOSECONDS:
			return hours * (ONE_HOUR_TO_NANO / ONE_NANO_SECOND);
		case MICROSECONDS:
			return hours * (ONE_HOUR_TO_NANO / ONE_MICRO_SECOND_TO_NANO);
		case MILLISECONDS:
			return hours * (ONE_HOUR_TO_NANO / ONE_MILLI_SECOND_TO_NANO);
		case SECONDS:
			return hours * (ONE_HOUR_TO_NANO / ONE_SECOND_TO_NANO);
		case MINUTES:
			return hours * (ONE_HOUR_TO_NANO / ONE_MINUTE_TO_NANO);
		case HOURS:
		default:
			return hours;
		case DAYS:
			return hours / (ONE_DAY_TO_NANO / ONE_HOUR_TO_NANO);
		}
	}

	public static double daysToUnit(double days, TimeUnit destinationUnit) {
		switch (destinationUnit) {
		case NANOSECONDS:
			return days * (ONE_DAY_TO_NANO / ONE_NANO_SECOND);
		case MICROSECONDS:
			return days * (ONE_DAY_TO_NANO / ONE_MICRO_SECOND_TO_NANO);
		case MILLISECONDS:
			return days * (ONE_DAY_TO_NANO / ONE_MILLI_SECOND_TO_NANO);
		case SECONDS:
			return days * (ONE_DAY_TO_NANO / ONE_SECOND_TO_NANO);
		case MINUTES:
			return days * (ONE_DAY_TO_NANO / ONE_MINUTE_TO_NANO);
		case HOURS:
			return days * (ONE_DAY_TO_NANO / ONE_HOUR_TO_NANO);
		case DAYS:
		default:
			return days;
		}
	}

	public static void main(String args[]) {
		double oneDayToNano = daysToUnit(1, TimeUnit.NANOSECONDS);
		double oneHourToNano = hoursToUnit(1, TimeUnit.NANOSECONDS);
		double oneMinuteToNano = minutesToUnit(1, TimeUnit.NANOSECONDS);
		double oneSecondToNano = secondsToUnit(1, TimeUnit.NANOSECONDS);
		double oneMilliToNano = millisToUnit(1, TimeUnit.NANOSECONDS);
		double oneMicroToNano = microsToUnit(1, TimeUnit.NANOSECONDS);
		double oneNanoToNano = nanosToUnit(1, TimeUnit.NANOSECONDS);

		System.out.println("oneDayToNano : " + oneDayToNano);
		System.out.println("oneHourToNano : " + oneHourToNano);
		System.out.println("oneMinuteToNano : " + oneMinuteToNano);
		System.out.println("oneSecondToNano : " + oneSecondToNano);
		System.out.println("oneMilliToNano : " + oneMilliToNano);
		System.out.println("oneMicroToNano : " + oneMicroToNano);
		System.out.println("oneNanoToNano : " + oneNanoToNano);

		double oneDayToSeconds = daysToUnit(1, TimeUnit.SECONDS);
		double oneHourToSeconds = hoursToUnit(1, TimeUnit.SECONDS);
		double oneMinuteToSeconds = minutesToUnit(1, TimeUnit.SECONDS);
		double oneSecondToSeconds = secondsToUnit(1, TimeUnit.SECONDS);
		double oneMilliToSeconds = millisToUnit(1, TimeUnit.SECONDS);
		double oneMicroToSeconds = microsToUnit(1, TimeUnit.SECONDS);
		double oneNanoToSeconds = nanosToUnit(1, TimeUnit.SECONDS);

		System.out.println("\noneDayToSeconds : " + oneDayToSeconds);
		System.out.println("oneHourToSeconds : " + oneHourToSeconds);
		System.out.println("oneMinuteToSeconds : " + oneMinuteToSeconds);
		System.out.println("oneSecondToSeconds : " + oneSecondToSeconds);
		System.out.println("oneMilliToSeconds : " + oneMilliToSeconds);
		System.out.println("oneMicroToSeconds : " + oneMicroToSeconds);
		System.out.println("oneNanoToSeconds : " + oneNanoToSeconds);
	}

}

 

Output

oneDayToNano : 8.64E13
oneHourToNano : 3.6E12
oneMinuteToNano : 6.0E10
oneSecondToNano : 1.0E9
oneMilliToNano : 1000000.0
oneMicroToNano : 1000.0
oneNanoToNano : 1.0

oneDayToSeconds : 86400.0
oneHourToSeconds : 3600.0
oneMinuteToSeconds : 60.0
oneSecondToSeconds : 1.0
oneMilliToSeconds : 0.001
oneMicroToSeconds : 1.0E-6
oneNanoToSeconds : 1.0E-9

In summary, time dimensions and being able to convert between them is essential for various Java applications. With the utility class provided, you can seamlessly convert time units with ease. Feel free to extend the TimeConverter class to include additional conversions or refine the existing ones to suit your specific needs.

 

Time is indeed of the essence in programming, and mastering its dimensions empowers you to build robust and efficient solutions.



Previous                                                    Next                                                    Home

No comments:

Post a Comment