Java String to Date toDate(Map filter, String field)

Here you can find the source of toDate(Map filter, String field)

Description

to Date

License

Open Source License

Declaration

public static Map<String, Object> toDate(Map<String, Object> filter, String field) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class Main {
    public static final int MODO_NOP = 0;
    public static final int MODO_DEFAULT_VALUE = 1;
    public static final int MODO_EXCEPTION = 2;

    public static Map<String, Object> toDate(Map<String, Object> filter, String field) throws Exception {
        return toDate(filter, field, null, MODO_NOP);
    }/*from www .  j  a  v  a  2  s  . co  m*/

    public static Map<String, Object> toDate(Map<String, Object> filter, String field, Object dv, int modo)
            throws Exception {
        System.out.println("### MAPPER UTILS: field=" + field);

        if (filter.containsKey(field)) {
            Object data = filter.get(field);
            System.out.println("### MAPPER UTILS: data=" + data);

            if (data instanceof java.lang.String) {
                String fechaRecibida = (String) data;
                System.out.println("TO_DATE_STRING:(" + field + "):" + data.toString());
                Date fechaDevuelta;
                //POSIBLES FORMATOS:
                SimpleDateFormat formatoFechaYMD = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                SimpleDateFormat formatoFechaDMY = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss");
                SimpleDateFormat formatoFechaYMD2 = new SimpleDateFormat("yyyy-MM-dd");
                //TODO ver si funciona con fechas con / y con -
                formatoFechaYMD.setLenient(false); //siempre a falso para que el usuario no meta fechas no validas.
                formatoFechaDMY.setLenient(false);

                try {
                    fechaDevuelta = formatoFechaYMD.parse(fechaRecibida);
                    filter.put(field, fechaDevuelta);
                    System.out.println("YMD->" + fechaDevuelta);
                } catch (Exception e) {
                    try {
                        fechaDevuelta = formatoFechaDMY.parse(fechaRecibida);
                        filter.put(field, fechaDevuelta);
                        System.out.println("DMY->" + fechaDevuelta);
                    } catch (Exception x) {
                        try {
                            fechaDevuelta = formatoFechaYMD2.parse(fechaRecibida);
                            filter.put(field, fechaDevuelta);
                            System.out.println("YMD2->" + fechaDevuelta);
                        } catch (Exception x2) {
                            System.out.println("->DEFAULT!!!");
                            switch (modo) {
                            case MODO_DEFAULT_VALUE:
                                filter.put(field, dv);
                                break;
                            case MODO_EXCEPTION:
                                throw new Exception("Formato de fecha no permitido: " + fechaRecibida);
                            }
                        }
                    }
                }
            } else if (data instanceof java.lang.Long) {
                //TODO ver si es mejopr crea un objeto date
                Date date = new Date((Long) data);
                //SimpleDateFormat formatoFecha = new SimpleDateFormat("y-M-d");
                //formatoFecha.setLenient(false);
                //filter.put(field, formatoFecha.format(date));
                filter.put(field, date);
                System.out.println("TO_DATE_LONG:(" + field + "):" + data.toString() + "->" + date);
            } else {
                //LANZO UN ERROR!
                System.out.println(
                        "TO_DATE_DESCONOCIDO:(" + field + "):" + data.toString() + " " + data.getClass().getName());
            }
        } else {
            modo(filter, field, dv, modo);
            System.out.println("TO_DATE_DEFAULT!!!:(" + field + "):" + filter.get(field));
        }
        return filter;

    }

    private static void modo(Map<String, Object> filter, String field, Object dv, int modo) throws Exception {
        switch (modo) {
        case MODO_DEFAULT_VALUE:
            filter.put(field, dv);
            break;
        case MODO_EXCEPTION:
            throw new Exception("No existe el parametro: " + field);
        }
    }
}

Related

  1. toDate(final String date, final String time)
  2. toDate(final String dateString, final String format)
  3. toDate(final String dateString, final String pattern)
  4. toDate(int date, String timeFormat)
  5. toDate(long value, String format)
  6. toDate(Object v, String format, Date defaultValue)
  7. toDate(Object value, String format)
  8. toDate(String createdAt)
  9. toDate(String d)