/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package com.megatim.queryadhoc.queries.metadata; import com.megatim.queryadhoc.exceptions.LocalDateTimeValueParseError; import java.time.LocalDateTime; import java.time.format.DateTimeParseException; /** * * @author ASUS */ public class LocalDateTimeValue { private final String format; private final Object value; public LocalDateTimeValue(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 monthIndex, int dayIndex, String stringValue) throws LocalDateTimeValueParseError { if (yearIndex >= 0 && monthIndex >= 0 && dayIndex >= 0) { int annee = Integer.parseInt(stringValue.substring(yearIndex, yearIndex + 4)); int mois = Integer.parseInt(stringValue.substring(monthIndex, monthIndex + 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); } }