Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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);
    }
 
}