返回顶部
扩大
缩小

Heaton

时间转换工具类

拿走不谢

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

/**
 * @author Heaton
 * @date 2018/5/8 11:57
 * @describe 时间工具类
 */
@SuppressWarnings("all")
public class DateUtil {

	private static ThreadLocal<SimpleDateFormat> listThreadLocalYM = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyyMM");
		}
	};
	private static ThreadLocal<SimpleDateFormat> listThreadLocalY_M = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM");
		}
	};
	private static ThreadLocal<SimpleDateFormat> listThreadLocalY_M_D = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM-dd");
		}
	};
	private static ThreadLocal<SimpleDateFormat> listThreadLocalY_M_D_H_M_S = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
	};

	/**
	 * @param [format]
	 * @return java.text.SimpleDateFormat
	 * @date 2018/5/8 12:04
	 * @describe 成功返回{@link SimpleDateFormat  根据需要格式转换需要的时间格式化}
	 */
	public static SimpleDateFormat getFormat(String format) {
		SimpleDateFormat sdf;
		if ("yyyyMM".equalsIgnoreCase(format)) {
			sdf = listThreadLocalYM.get();
		} else if ("yyyy-MM".equalsIgnoreCase(format)) {
			sdf = listThreadLocalY_M.get();
		} else if ("yyyy-MM-dd".equalsIgnoreCase(format)) {
			sdf = listThreadLocalY_M_D.get();
		} else if ("yyyy-MM-dd HH:mm:ss".equalsIgnoreCase(format)) {
			sdf = listThreadLocalY_M_D_H_M_S.get();
		} else {
			sdf = listThreadLocalY_M_D.get();
		}
		return sdf;
	}

	/**
	 * 时间戳转换成日期格式字符串
	 *
	 * @param seconds   精确到秒的字符串
	 * @param formatStr
	 * @return
	 */
	public static String timeStamp2Date(String seconds, String format) {
		if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
			return "";
		}
		if (format == null || format.isEmpty()) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = getFormat(format);
		return sdf.format(new Date(Long.valueOf(seconds + "000")));
	}

	/**
	 * @param [date_str 字符串日期, format 如:yyyy-MM-dd HH:mm:ss]
	 * @return java.lang.String
	 * @date 2018/5/8 12:07
	 * @describe 成功返回{@link String  }日期格式字符串转换成时间戳
	 */
	public static String date2TimeStamp(String date_str, String format) {
		try {
			SimpleDateFormat sdf = getFormat(format);
			return String.valueOf(sdf.parse(date_str).getTime() / 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * @param []
	 * @return java.lang.String
	 * @date 2018/5/8 12:06
	 * @describe 成功返回{@link String }取得当前时间戳(精确到秒)
	 */
	public static String timeStamp() {
		long time = System.currentTimeMillis();
		String t = String.valueOf(time / 1000);
		return t;
	}

	/**
	 * @param [date]
	 * @return java.lang.String
	 * @date 2018/5/7 15:02
	 * @describe 成功返回{@link String} 时间格式的字符串
	 */
	public static String date2String(Date date, String format) {
		SimpleDateFormat sdf = getFormat(format);
		String str = sdf.format(date);
		return str;
	}

	/**
	 * @param [time1, time2, format]
	 * @return java.lang.Boolean
	 * @date 2018/5/7 19:14
	 * @describe 成功返回{@link boolean} 比较时间
	 */
	public static Boolean equalsTime(Date time1, Date time2, String format) {
		SimpleDateFormat sdf = getFormat(format);
		String l1 = sdf.format(time1);
		String l2 = sdf.format(time2);
		return l1.equalsIgnoreCase(l2);
	}

	/**
	 * @param [time1, time2, format]
	 * @return java.lang.Boolean
	 * @date 2018/5/7 19:14
	 * @describe 成功返回{@link Long} 比较时间
	 */
	public static Integer equalsTimeLong(Date time1, Date time2, String format) {
		SimpleDateFormat sdf = getFormat(format);
		Integer i1 = Integer.parseInt(sdf.format(time1));
		Integer i2 = Integer.parseInt(sdf.format(time2));
		return i1 - i2;
	}

	/**
	 * @param [endTime]
	 * @return java.util.Date
	 * @date 2018/5/8 12:12
	 * @describe 成功返回{@link Date}返回上月的时间
	 */
	public static Date upMonth(Date endTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(endTime); // 设置为当前时间
		calendar.add(Calendar.MONTH, -1); // 设置为上一个月
		return calendar.getTime();
	}

	/**
	 * @param [endTime]
	 * @return java.util.Date
	 * @date 2018/5/8 12:12
	 * @describe 成功返回{@link Date}返回下月的时间
	 */
	public static Date downMonth(Date endTime) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(endTime); // 设置为当前时间
		calendar.add(Calendar.MONTH, 1); // 设置为下一个月
		return calendar.getTime();
	}

	public static void main(String[] args) {
		String timeStamp = timeStamp();
		System.out.println("timeStamp=" + timeStamp); //运行输出:timeStamp=1470278082
		System.out.println(System.currentTimeMillis());//运行输出:1470278082980
		//该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数

		String date = timeStamp2Date(timeStamp, "yyyy-MM-dd HH:mm:ss");
		System.out.println("date=" + date);//运行输出:date=2016-08-04 10:34:42

		String timeStamp2 = date2TimeStamp(date, "yyyy-MM-dd HH:mm:ss");
		System.out.println(timeStamp2);  //运行输出:1470278082

		System.out.println(date2String(downMonth(new Date()), "yyyy-MM"));//下月
	}


}

posted on 2018-05-17 14:43  咘雷扎克  阅读(107)  评论(0)    收藏  举报

导航