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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.megatim.fdxcommons.tools.database.queries.metadata;
 
import com.megatim.fdxcommons.tools.database.exceptions.LocalDateTimeValueParseError;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
 
/**
 *
 * @author ASUS
 */
public class InsertionLocalDateTimeValue {
 
    private final String format;
    private final Object value;
 
    public InsertionLocalDateTimeValue(String format, Object value) {
        this.format = format;
        this.value = value;
    }
 
    public LocalDateTime value() throws LocalDateTimeValueParseError {
        final String stringValue = value.toString().trim();
 
        try {
 
            if (format.length() == 4 && format.equals("yyyy") && stringValue.matches("\\d{4}")) {
                return LocalDateTime.of(Integer.parseInt(stringValue), 1, 1, 0, 0, 0);
 
            } else if ((format.length() == 6 && stringValue.length() == 6) || (format.length() == 7) && stringValue.length() == 7) {
 
                int monthIndex = index(format, "MM");
                int yearIndex = index(format, "yyyy");
                
                return date(yearIndex, monthIndex, stringValue);
                
            } else if ((format.length() == 8 && stringValue.length() == 8)
                    || (format.length() == 10 && stringValue.length() == 10)) {
 
                int dayIndex = index(format, "dd");
                int monthIndex = index(format, "MM");
                int yearIndex = index(format, "yyyy");
 
                return date(yearIndex, monthIndex, dayIndex, stringValue);
            }
 
        } 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);
    }
 
    private int index(String format, String subSequence) {
        return format.indexOf(subSequence);
    }
    private LocalDateTime date(int yearIndex, int monthIdex, int dayIndex, String stringValue) throws LocalDateTimeValueParseError {
 
        if (yearIndex >= 0 && monthIdex >= 0 && dayIndex >= 0) {
            int annee = Integer.parseInt(stringValue.substring(yearIndex, yearIndex + 4));
            int mois = Integer.parseInt(stringValue.substring(monthIdex, monthIdex + 2));
            int jour = Integer.parseInt(stringValue.substring(dayIndex, dayIndex + 2));
 
            if (mois >= 1 && mois <= 12 && jour >= 1 && jour <= 31) {
                if ((mois == 1 || mois == 3 || mois == 5 || mois == 7 || mois == 8 || mois == 10 || mois == 12) && jour <= 31//Les mois ayant 31 jours
                        || ((mois == 4 || mois == 6 || mois == 9 || mois == 11) && jour <= 30)//les ayant 30 jours
                        || (mois == 2 && (annee % 4 == 0 && jour <= 29 || jour <= 28))) { //Le mois de fĂ©vrier
 
                    return LocalDateTime.of(annee, mois, jour, 0, 0, 0, 0);
                }
            }
        }
        throw new LocalDateTimeValueParseError("Impossible de parser la valeur " + stringValue + " au format " + format);
    }
 
    private LocalDateTime date(int yearIndex, int monthIdex, String stringValue) throws LocalDateTimeValueParseError {
 
        if (yearIndex >= 0 && monthIdex >= 0) {
            int annee = Integer.parseInt(stringValue.substring(yearIndex, yearIndex + 4));
            int mois = Integer.parseInt(stringValue.substring(monthIdex, monthIdex + 2));
            int jour = 1;
 
            if (mois >= 1 && mois <= 12) {
                return LocalDateTime.of(annee, mois, jour, 0, 0, 0, 0);
            }
        }
        throw new LocalDateTimeValueParseError("Impossible de parser la valeur " + stringValue + " au format " + format);
    }
}