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

com.xiaoleilu.hutool.convert.impl.SqlDateConverter Maven / Gradle / Ivy

package com.xiaoleilu.hutool.convert.impl;

import java.sql.Date;
import java.util.Calendar;

import com.xiaoleilu.hutool.convert.AbstractConverter;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * SQL日期转换器
 * 
 * @author Looly
 *
 */
public class SqlDateConverter extends AbstractConverter {

	/** 日期格式化 */
	private String format;

	/**
	 * 获取日期格式
	 * 
	 * @return 设置日期格式
	 */
	public String getFormat() {
		return format;
	}

	/**
	 * 设置日期格式
	 * 
	 * @param format 日期格式
	 */
	public void setFormat(String format) {
		this.format = format;
	}

	@Override
	protected Date convertInternal(Object value) {
		// Handle Calendar
		if (value instanceof Calendar) {
			return new Date(((Calendar) value).getTime().getTime());
		}

		// Handle Long
		if (value instanceof Long) {
			//此处使用自动拆装箱
			return new Date((Long)value);
		}

		final String valueStr = convertToStr(value);
		try {
			final long date = StrUtil.isBlank(format) ? DateUtil.parse(valueStr).getTime() : DateUtil.parse(valueStr, format).getTime();
			return new Date(date);
		} catch (Exception e) {
			// Ignore Exception
		}
		return null;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy