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

com.xwzhou.commons.lang.helper.DateHelper Maven / Gradle / Ivy

The newest version!
package com.xwzhou.commons.lang.helper;

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

public final class DateHelper {

	public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

	private static ThreadLocal threadLocal = new ThreadLocal();

	public static SimpleDateFormat getDateFormat(String format) {
		SimpleDateFormat df = threadLocal.get();
		if (df == null) {
			df = new SimpleDateFormat(format);
			threadLocal.set(df);
		}
		return df;
	}

	public static final String format(Date date) throws ParseException {
		return format(date, DEFAULT_FORMAT);
	}

	public static final String format(Date date, String format) throws ParseException {
		return getDateFormat(format).format(date);
	}

	public static final Date parse(String date) throws ParseException {
		return parse(date, DEFAULT_FORMAT);
	}

	public static final Date parse(String date, String format) throws ParseException {
		return getDateFormat(format).parse(date);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy