mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
* Added-time-converter-algorithm * Updated-java-docs * Fixed-formatting-issues * Enhanced-unit-tests * Fixed-failing-check-style --------- Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
98 lines
3.2 KiB
Java
98 lines
3.2 KiB
Java
package com.thealgorithms.conversions;
|
|
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* A utility class to convert between different units of time.
|
|
*
|
|
* <p>This class supports conversions between the following units:
|
|
* <ul>
|
|
* <li>seconds</li>
|
|
* <li>minutes</li>
|
|
* <li>hours</li>
|
|
* <li>days</li>
|
|
* <li>weeks</li>
|
|
* <li>months (approximated as 30.44 days)</li>
|
|
* <li>years (approximated as 365.25 days)</li>
|
|
* </ul>
|
|
*
|
|
* <p>The conversion is based on predefined constants in seconds.
|
|
* Results are rounded to three decimal places for consistency.
|
|
*
|
|
* <p>This class is final and cannot be instantiated.
|
|
*
|
|
* @see <a href="https://en.wikipedia.org/wiki/Unit_of_time">Wikipedia: Unit of time</a>
|
|
*/
|
|
public final class TimeConverter {
|
|
|
|
private TimeConverter() {
|
|
// Prevent instantiation
|
|
}
|
|
|
|
/**
|
|
* Supported time units with their equivalent in seconds.
|
|
*/
|
|
private enum TimeUnit {
|
|
SECONDS(1.0),
|
|
MINUTES(60.0),
|
|
HOURS(3600.0),
|
|
DAYS(86400.0),
|
|
WEEKS(604800.0),
|
|
MONTHS(2629800.0), // 30.44 days
|
|
YEARS(31557600.0); // 365.25 days
|
|
|
|
private final double seconds;
|
|
|
|
TimeUnit(double seconds) {
|
|
this.seconds = seconds;
|
|
}
|
|
|
|
public double toSeconds(double value) {
|
|
return value * seconds;
|
|
}
|
|
|
|
public double fromSeconds(double secondsValue) {
|
|
return secondsValue / seconds;
|
|
}
|
|
}
|
|
|
|
private static final Map<String, TimeUnit> UNIT_LOOKUP
|
|
= Map.ofEntries(Map.entry("seconds", TimeUnit.SECONDS), Map.entry("minutes", TimeUnit.MINUTES), Map.entry("hours", TimeUnit.HOURS), Map.entry("days", TimeUnit.DAYS), Map.entry("weeks", TimeUnit.WEEKS), Map.entry("months", TimeUnit.MONTHS), Map.entry("years", TimeUnit.YEARS));
|
|
|
|
/**
|
|
* Converts a time value from one unit to another.
|
|
*
|
|
* @param timeValue the numeric value of time to convert; must be non-negative
|
|
* @param unitFrom the unit of the input value (e.g., "minutes", "hours")
|
|
* @param unitTo the unit to convert into (e.g., "seconds", "days")
|
|
* @return the converted value in the target unit, rounded to three decimals
|
|
* @throws IllegalArgumentException if {@code timeValue} is negative
|
|
* @throws IllegalArgumentException if either {@code unitFrom} or {@code unitTo} is not supported
|
|
*/
|
|
public static double convertTime(double timeValue, String unitFrom, String unitTo) {
|
|
if (timeValue < 0) {
|
|
throw new IllegalArgumentException("timeValue must be a non-negative number.");
|
|
}
|
|
|
|
TimeUnit from = resolveUnit(unitFrom);
|
|
TimeUnit to = resolveUnit(unitTo);
|
|
|
|
double secondsValue = from.toSeconds(timeValue);
|
|
double converted = to.fromSeconds(secondsValue);
|
|
|
|
return Math.round(converted * 1000.0) / 1000.0;
|
|
}
|
|
|
|
private static TimeUnit resolveUnit(String unit) {
|
|
if (unit == null) {
|
|
throw new IllegalArgumentException("Unit cannot be null.");
|
|
}
|
|
TimeUnit resolved = UNIT_LOOKUP.get(unit.toLowerCase(Locale.ROOT));
|
|
if (resolved == null) {
|
|
throw new IllegalArgumentException("Invalid unit '" + unit + "'. Supported units are: " + UNIT_LOOKUP.keySet());
|
|
}
|
|
return resolved;
|
|
}
|
|
}
|