package com.megatim.fdxcommons.tools.database.queries.metadata;
|
|
import com.megatim.fdxcommons.tools.database.exceptions.LocalDateTimeValueParseError;
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.time.LocalTime;
|
import java.time.YearMonth;
|
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeParseException;
|
|
/**
|
*
|
* @author Gabuntu
|
*/
|
public class CriteriaLocalDateTimeValue {
|
|
private final String format;
|
private final Object value;
|
|
public CriteriaLocalDateTimeValue(String format, Object value) {
|
this.format = format;
|
this.value = value;
|
}
|
|
public LocalDateTime value() throws LocalDateTimeValueParseError {
|
final String stringValue = value.toString();
|
|
try {
|
if ((format.length() == 8 || format.length() == 10) && format.contains("yyyy") && format.contains("MM") && format.contains("dd")) {
|
|
LocalDate localDate = LocalDate.parse(stringValue, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
return localDate.atTime(LocalTime.MIN);
|
|
} else if ((format.length() == 6 || format.length() == 8) && format.contains("yyyy") && format.contains("MM")) {
|
YearMonth yearMonth = YearMonth.parse(stringValue, DateTimeFormatter.ofPattern("yyyy-MM"));
|
return LocalDateTime.from(yearMonth.atDay(1));
|
|
} else if (format.length() == 4 && format.equals("yyyy")) {
|
return LocalDateTime.of(Integer.parseInt(stringValue), 1, 1, 0, 0, 0, 0);
|
}
|
|
} catch (DateTimeParseException e) {
|
throw new LocalDateTimeValueParseError("Impossible de parser la valeur " + value + " au format " + format);
|
}
|
|
throw new LocalDateTimeValueParseError("Impossible de parser la valeur " + value + " au format " + format);
|
}
|
|
}
|