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

info.unterrainer.commons.jreutils.DateUtils Maven / Gradle / Ivy

There is a newer version: 0.3.15
Show newest version
package info.unterrainer.commons.jreutils;

import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;

import lombok.experimental.UtilityClass;

@UtilityClass
public class DateUtils {

	public static DateTimeFormatter ISO_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

	public static LocalDateTime nowUtc() {
		return LocalDateTime.now(ZoneOffset.UTC);
	}

	public static Long utcLocalDateTimeToEpoch(final LocalDateTime utc) {
		return utc.atZone(ZoneId.of("UTC")).toInstant().toEpochMilli();
	}

	public static LocalDateTime epochToUtcLocalDateTime(final Long epoch) {
		return Instant.ofEpochMilli(epoch).atZone(ZoneId.of("UTC")).toLocalDateTime();
	}

	public static LocalDateTime fileTimeToUtcLocalDateTime(final FileTime time) {
		return time.toInstant().atZone(ZoneId.of("UTC")).toLocalDateTime();
	}

	public static int getWeekOf(final LocalDateTime dateTime) {
		return dateTime.get(WeekFields.ISO.weekOfWeekBasedYear());
	}

	public static String localDateTimeToIso(final LocalDateTime localDateTime) {
		return localDateTime.format(ISO_FORMATTER);
	}

	public static String zonedDateTimeToIso(final ZonedDateTime zonedDateTime) {
		return zonedDateTime.format(ISO_FORMATTER);
	}

	/**
	 * Converts a UTC-issued LocalDateTime to a LocalDateTime issued by the
	 * time-zone specified by the timeZoneIdString.
	 *
	 * @param utc              the input UTC-issued LocalDateTime to convert
	 * @param timeZoneIdString a String describing the target time-zone (for example
	 *                         "Europe/Vienna")
	 * @return the new LocalDateTime
	 */
	public static LocalDateTime utcLocalDateTimeAtZone(final LocalDateTime utc, final String timeZoneIdString) {
		ZonedDateTime utcZoned = ZonedDateTime.of(utc, ZoneId.of("UTC"));
		return utcZoned.withZoneSameInstant(ZoneId.of(timeZoneIdString)).toLocalDateTime();
	}

	public static String utcLocalDateTimeToLocalFormat(final LocalDateTime utc, final String timeZoneIdString) {
		return utcLocalDateTimeToLocalFormat(utc, timeZoneIdString, ISO_FORMATTER);
	}

	public static String utcLocalDateTimeToLocalFormat(final LocalDateTime utc, final String timeZoneIdString,
			final DateTimeFormatter dateTimeFormatter) {
		LocalDateTime local = utcLocalDateTimeAtZone(utc, timeZoneIdString);
		return local.format(dateTimeFormatter);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy