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

com.firefly.utils.json.parser.DateParser Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.firefly.utils.json.parser;

import java.util.Date;

import com.firefly.utils.json.JsonReader;
import com.firefly.utils.json.Parser;
import com.firefly.utils.time.SafeSimpleDateFormat;

public class DateParser implements Parser {

	private SafeSimpleDateFormat safeSimpleDateFormat = SafeSimpleDateFormat.defaultDateFormat;

	public DateParser() {
	}

	public DateParser(String datePattern) {
		this.safeSimpleDateFormat = new SafeSimpleDateFormat(datePattern);
	}

	@Override
	public Object convertTo(JsonReader reader, Class clazz) {
		if (reader.isNull())
			return null;

		try {
			reader.mark(1024);
			if (reader.isString()) {
				reader.reset();
				String s = reader.readString();
				return safeSimpleDateFormat.parse(s);
			} else {
				reader.reset();
				long timestamp = reader.readLong();
				return new Date(timestamp);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy