All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mokung.pomegranate.jackson.handler.getter.DateGetter Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.mokung.pomegranate.jackson.handler.getter;

import java.text.SimpleDateFormat;
import java.util.*;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.POJONode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.mokung.pomegranate.jackson.handler.Getter;

/**
 *
 * @author mokung
 */
public class DateGetter implements Getter {

    @Override
    public Date getter(JsonNode valueNode, ObjectMapper mapper, boolean cast) {
        if (valueNode instanceof POJONode) {
            Object value = ((POJONode)valueNode).getPojo();
            if (value instanceof Date) {
                return (Date)value;
            }
            if (value instanceof Calendar) {
                return ((Calendar)value).getTime();
            }
        }
        if (!cast || valueNode == null || valueNode instanceof NullNode) {
            return null;
        }
        if (valueNode instanceof NumericNode) {
            long time = valueNode.asLong();
            return new Date(time);
        }
        if (valueNode instanceof TextNode) {
            String value = valueNode.asText();
            Long longValue = LongGetter.stringToLong(value);
            if (longValue != null) {
                return new Date(longValue);
            }
            return stringToDate(value);
        }
        if (valueNode instanceof POJONode) {
            Object value = ((POJONode)valueNode).getPojo();
            if (value == null) {
                return null;
            }
            Long longValue = LongGetter.stringToLong(value.toString());
            if (longValue != null) {
                return new Date(longValue);
            }
            return stringToDate(value.toString());
        }

        return null;
    }

    protected static final List DATE_FORMAT_LIST =
        Arrays.asList(new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"), new SimpleDateFormat("yyyyMMddHHmmssSSSZ"),
            new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"),
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS"), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"),
            new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.SIMPLIFIED_CHINESE),
            new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.TRADITIONAL_CHINESE));

    protected static final Date stringToDate(String value) {
        if (value == null || value.length() <= 0 || value.trim().length() <= 0) {
            return null;
        }
        for (SimpleDateFormat format : DATE_FORMAT_LIST) {
            try {
                return format.parse(value);
            } catch (Exception e) {
                // ignore
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy