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

com.sghd.common.utils.time.DateThreadUtils Maven / Gradle / Ivy

The newest version!
package com.sghd.common.utils.time;

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

public abstract class DateThreadUtils {

	public final static String YYYYMMDD = "yyyy-MM-dd";
	public final static String YYYYMM = "yyyy-MM";
	public final static String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";

	private final static Object lock = new Object();

	private static ThreadLocal dateFormatThreadLocal = new ThreadLocal();

	private static SimpleDateFormat get() {
		SimpleDateFormat dateFormat = dateFormatThreadLocal.get();
		if (dateFormat == null) {
			synchronized (lock) {
				dateFormat = dateFormatThreadLocal.get();
				if (dateFormat == null) {
					dateFormat = new SimpleDateFormat();
					dateFormatThreadLocal.set(dateFormat);
				}
			}
		}
		// System.out.println(Thread.currentThread().getName());

		return dateFormat;
	}

	public static Date trystr2date(String date, String pattern) throws ParseException {
		SimpleDateFormat dateFormat = get();
		dateFormat.applyPattern(pattern);
		return dateFormat.parse(date);
	}

	public static Date str2date(String date, String pattern) {
		SimpleDateFormat dateFormat = get();
		dateFormat.applyPattern(pattern);
		try {
			return dateFormat.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}

	public static String date2str(Date date, String pattern) {
		SimpleDateFormat dateFormat = get();
		dateFormat.applyPattern(pattern);
		return dateFormat.format(date);

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy