All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.base.util.TemporalUtils Maven / Gradle / Ivy

There is a newer version: 1.17.5
Show newest version
package com.xlrit.gears.base.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;

import static com.xlrit.gears.base.util.StringUtils.*;

public class TemporalUtils {

	public static LocalDate parseDate(String dateText) {
		return LocalDate.parse(dateText);
	}

	public static LocalDate parseDate(String dateText, DateTimeFormatter formatter) {
		return LocalDate.parse(dateText, formatter);
	}

	public static LocalTime parseTime(String timeText) {
		return parseTime(timeText, DateTimeFormatter.ISO_TIME);
	}

	public static LocalTime parseTime(String timeText, DateTimeFormatter formatter) {
		TemporalAccessor temporalAccessor = formatter.parseBest(timeText, OffsetTime::from, LocalTime::from);
		if (temporalAccessor instanceof OffsetTime offsetTime) {
			throw new IllegalArgumentException("Time with offset is not supported: " + offsetTime);
		}
		if (temporalAccessor instanceof LocalTime localTime) {
			return localTime;
		}
		throw new IllegalStateException();
	}

	public static OffsetDateTime parseDateTime(String dateTimeText) {
		return parseDateTime(dateTimeText, DateTimeFormatter.ISO_DATE_TIME);
	}

	public static OffsetDateTime parseDateTime(String dateTimeText, DateTimeFormatter formatter) {
		TemporalAccessor temporalAccessor = formatter.parseBest(dateTimeText, OffsetDateTime::from, ZonedDateTime::from, LocalDateTime::from);
		if (temporalAccessor instanceof OffsetDateTime offsetDateTime) {
			return offsetDateTime;
		}
		if (temporalAccessor instanceof ZonedDateTime zonedDateTime) {
			return zonedDateTime.toOffsetDateTime();
		}
		if (temporalAccessor instanceof LocalDateTime localDateTime) {
			return localDateTime.atOffset(ZoneOffset.UTC);
		}
		throw new IllegalStateException();
	}

	public static void assertRequireEqualType(Temporal a, Temporal b, String functionName) {
		if (a.getClass() == b.getClass())
			throw new IllegalArgumentException(String.format("Function %s requires input types to be equal, but got %s and %s.", toSnakeCase(functionName), a.getClass().getSimpleName(), b.getClass().getSimpleName()));
	}

	public static void assertTimeRestriction(Temporal start, Temporal end, String functionName, String unitName) {
		if (isTime(start) ^ isTime(end))
			throw new IllegalArgumentException(String.format("Function %s cannot determine number of %s between a time and a date or datetime.", toSnakeCase(functionName), unitName));
	}

	public static void assertRequireDates(Temporal start, Temporal end, String functionName) {
		assertRequireDate(start, functionName);
		assertRequireDate(end, functionName);
	}

	public static void assertRequireDate(Temporal temp, String functionName) {
		if(!(isDateTime(temp) || isDate(temp)))
			throw new IllegalArgumentException(String.format("Function %s only accepts dates. Type %s is provided.", toSnakeCase(functionName), temp.getClass().getSimpleName()));
	}

	public static void assertRequireTimesOrDates(Temporal start, Temporal end, String functionName) {
		assertRequireTimeOrDate(start, functionName);
		assertRequireTimeOrDate(end, functionName);
	}

	public static void assertRequireTimeOrDate(Temporal temp, String functionName) {
		if(!(isDateTime(temp) || isDate(temp) || isTime(temp)))
			throw new IllegalArgumentException(String.format("Function %s only accepts dates or time. Type %s is provided.", toSnakeCase(functionName), temp.getClass().getSimpleName()));
	}

	private static boolean isTime(Temporal temp) { return temp.getClass() == LocalTime.class; }
	private static boolean isDateTime(Temporal temp) { return temp.getClass() == OffsetDateTime.class; }
	private static boolean isDate(Temporal temp) { return temp.getClass() == LocalDate.class; }

	// === used by tests === //

	public static OffsetDateTime createUtcDateTime(int year, int month, int dayOfMonth, int hour, int minute) {
		return OffsetDateTime.of(year, month, dayOfMonth, hour, minute, 0, 0, ZoneOffset.UTC);
	}

	public static OffsetDateTime createUtcDateTime(int year, int month, int dayOfMonth, int hour, int minute, int second) {
		return OffsetDateTime.of(year, month, dayOfMonth, hour, minute, second, 0, ZoneOffset.UTC);
	}

	public static OffsetDateTime createUtcDateTime(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanos) {
		return OffsetDateTime.of(year, month, dayOfMonth, hour, minute, second, nanos, ZoneOffset.UTC);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy