/*
 * Copyright 2014 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at http://www.eclipse.org/legal/epl-v10.html
 */
package io.liveoak.pgsql.meta;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;

/**
 * @author <a href="mailto:marko.strukelj@gmail.com">Marko Strukelj</a>
 */
public class ValueConverter {

    public static int toInt(Object val) {
        if (val == null) {
            throw new IllegalArgumentException("val == null");
        }
        if (val instanceof Number) {
            return ((Number) val).intValue();
        } else {
            return Integer.parseInt(String.valueOf(val));
        }
    }

    public static Timestamp toTimestamp(Object val) {
        if (val == null) {
            throw new IllegalArgumentException("val == null");
        }
        if (val instanceof Long || val instanceof Integer) {
            return new Timestamp(((Number) val).longValue());
        }
        if (val instanceof String) {
            return parseIsoDateTime((String) val);
        }
        if (val instanceof Timestamp) {
            return (Timestamp) val;
        }
        if (val instanceof Date) {
            return new Timestamp(((Date) val).getTime());
        }
        throw new IllegalArgumentException("Unsupported type: " + val.getClass());
    }

    private static Timestamp parseIsoDateTime(String val) {
        try {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
            LocalDateTime ldt = LocalDateTime.parse(val, dtf);
            Timestamp.valueOf(ldt);
        } catch (DateTimeParseException ignored) {}

        try {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
            LocalDateTime ldt = LocalDateTime.parse(val, dtf);
            Timestamp.valueOf(ldt);
        } catch (DateTimeParseException ignored) {}

        try {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime ldt = LocalDateTime.parse(val, dtf);
            Timestamp.valueOf(ldt);
        } catch (DateTimeParseException ignored) {}

        throw new IllegalArgumentException("Value could not be parsed as ISO DateTime: " + val);
    }
}

@@ -252,24 +252,80 @@
 me.LocalDate
+;%0Aimport java.time.LocalDateTime;%0Aimport java.time.Local
 Time;%0Aimport
@@ -544,16 +544,299 @@
 rter %7B%0A%0A
+    private static DateTimeFormatter ISO_MILLIS = DateTimeFormatter.ofPattern(%22yyyy-MM-dd'T'HH:mm:ss.SSS%22);%0A    private static DateTimeFormatter ISO_DATE_TIME = DateTimeFormatter.ISO_LOCAL_DATE_TIME;%0A    private static DateTimeFormatter ISO_DATE = DateTimeFormatter.ISO_LOCAL_DATE;%0A%0A
     publ
@@ -1898,438 +1898,215 @@
     
-DateTimeFormatter dtf = DateTimeFormatter.ofPattern(%22yyyy-MM-dd'T'HH:mm:ss%22);%0A            LocalDateTime ldt = LocalDateTime.parse(val, dtf);%0A            Timestamp.valueOf(ldt);%0A        %7D catch (DateTimeParseException ignored) %7B%7D%0A%0A        try %7B%0A            DateTimeFormatter dtf = DateTimeFormatter.ofPattern(%22yyyy-MM-dd'T'HH:mm:ss%22);%0A            LocalDateTime ldt = LocalDateTime.parse(val, dtf);%0A            Timestamp.valueOf(ldt
+return Timestamp.valueOf(LocalDateTime.parse(val, ISO_MILLIS));%0A        %7D catch (DateTimeParseException ignored) %7B%7D%0A%0A        try %7B%0A            return Timestamp.valueOf(LocalDateTime.parse(val, ISO_DATE_TIME)
 );%0A 
@@ -2187,87 +2187,33 @@
     
-DateTimeFormatter dtf = DateTimeFormatter.ofPattern(%22yyyy-MM-dd%22);%0A            
+return Timestamp.valueOf(
 Loca
@@ -2225,15 +2225,12 @@
 Time
- ldt = 
+.of(
 Loca
@@ -2226,36 +2226,32 @@
 ime.of(LocalDate
-Time
 .parse(val, dtf)
@@ -2250,47 +2250,38 @@
 al, 
-dtf);%0A            Timestamp.valueOf(ldt
+ISO_DATE), LocalTime.MIDNIGHT)
 );%0A 

Remove SimpleDateFormat and use DateTimeFormatter instead + fix new DateTime API usage