60 lines
2.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.example.nto.utils;
import lombok.experimental.UtilityClass;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@UtilityClass
public class Utils {
private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss");
public static String profileFileName(long userId) {
return "profile--" + userId;
}
public static String generateUniqueName() {
return UUID.randomUUID().toString();
}
public static String nowTime(DataFormatType type) {
switch (type) {
case DATE_TIME: return LocalDateTime.now().format(DATE_TIME_FORMAT);
case DATE: return LocalDateTime.now().format(DATE_FORMAT);
case TIME: return LocalDateTime.now().format(TIME_FORMAT);
default: return "Произошла ошибка при форматировании даты или времени.";
}
}
public static LocalDateTime period(LocalDateTime dtStart, LocalDateTime dtEnd) {
// Возвращает разницу между двумя LocalDateTime
Period period = Period.between(dtStart.toLocalDate(), dtEnd.toLocalDate());
Duration duration = Duration.between(dtStart.toLocalTime(), dtEnd.toLocalTime());
LocalDate localDate = LocalDate.of(period.getYears(), period.getMonths(), period.getDays());
LocalTime localTime = LocalTime.of(duration.toHoursPart(), duration.toMinutesPart(), duration.toSecondsPart());
return LocalDateTime.of(localDate, localTime);
}
public static long periods(List<List<LocalDateTime>> periods) {
// Количество часов за определенные периоды.
long hours = 0;
for (List<LocalDateTime> period : periods) {
if (period.size() != 2) throw new IllegalStateException("Список с периодом должен содержать 2 элемента!");
hours += Duration.between(period.get(0), period.get(1)).toHours();
}
return hours;
}
public static String convertDistance(float distance) {
if (distance > 1000) return String.format("%.1f", distance / 1000) + " км";
else return String.format("%.1f", distance) + " м";
}
}