Java-时间转换工具类(Date与String互转)

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

/**
 * @Description date与String互转
 *
 * @Date 2020年10月24日
 *
 * @Version 1.0
 */
public class DateAndStringUtil {

	/**
	 * 判断字符串是否是日期格式(格式类型:yyyy-MM-dd)
	 *
	 * @param date_string
	 * @return
	 */
	public static boolean isDate(String date_string) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 这里要捕获一下异常信息
		try {
			format.parse(date_string);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// -----------------------------------------------------当前时间获取----------------------------------------//
	/** 获取当前时间(返回格式:yyyyMMddHHmmss) */
	public static String getTime() throws Exception {
		Date date = new Date();
		// 设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		// new Date()为获取当前系统时间
		String time = df.format(date);
		return time;
	}

	/**
	 * 获取当前时间(返回格式:yyyy-MM-dd HH:mm:ss)
	 *
	 * @return
	 * @throws Exception
	 */
	public static String getTime1() throws Exception {
		Date date = new Date();
		// 设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// new Date()为获取当前系统时间
		String time = df.format(date);
		return time;
	}

	/**
	 * 获取当前时间(返回格式:yyyy-MM-dd)
	 *
	 * @return
	 * @throws Exception
	 */
	public static String getTime2() throws Exception {
		String time = null;
		Date date = new Date();
		// 设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		// new Date()为获取当前系统时间
		time = df.format(date);
		return time;
	}


	/**
	 * 自定义格式
	 * 获取当前时间(返回格式:yyyy-MM-dd_HH)
	 *
	 * @return
	 * @throws Exception
	 */
	public static String getTime4(){
		String time = null;
		Date date = new Date();
		// 设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH");
		// new Date()为获取当前系统时间
		time = df.format(date);
		return time;
	}


	/**
	 * 获取当天日期
	 *
	 * @return
	 */
	public static Date getTime3() throws Exception {
		Date date = new Date();
		// 设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			String time = df.format(date);
			// 获取时间
			date = df.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 自定义格式
	 * 获取当前时间(返回格式:yyyy-MM-dd HH:mm)
	 *
	 * @return
	 * @throws Exception
	 */
	public static String getTime5(){
		String time = null;
		Date date = new Date();
		// 设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		// new Date()为获取当前系统时间
		time = df.format(date);
		return time;
	}


	/**
	 * 获取当前时间(返回格式:yyyy-MM-dd)
	 *
	 * @return
	 * @throws Exception
	 */
	public static String getTime6(){
		String time = null;
		Date date = new Date();
		// 设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		// new Date()为获取当前系统时间
		time = df.format(date);
		return time;
	}


	/**
	 * 获取过去某一天的日期 yyyy-MM-dd
	 *
	 * @return
	 */
	public static String lastDay(int num) throws Exception {
		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -num); // 设置为前num天
		dBefore = calendar.getTime(); // 得到前num天的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
		String defaultStartDate = sdf.format(dBefore); // 格式化前num天
		return defaultStartDate;
	}

	/**
	 * 获取 多少年前/后 的时间
	 * @param year 正数代表未来,负数代表过去
	 * @return
	 */
	public static String getYearMonthDay(int year) {

		Calendar now = Calendar.getInstance();
		now.add(Calendar.YEAR, year); // 现在时间是传入year年后
		Date time = now.getTime();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String format = dateFormat.format(time);
		return format;
	}

	// -----------------------------------------------------String转date----------------------------------------//
	/**
	 * String转date(字符串格式:yyyy-MM-dd)
	 *
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static Date StringToDate1(String str) throws Exception {
		// 设置日期格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(str);
		return date;
	}

	/**
	 * 字符串传date(字符串格式:yyyyMMddhhmmss)
	 *
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static Date StringToDate2(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		Date date = sdf.parse(str);
		return date;
	}

	/**
	 * 字符串传date(字符串格式:yyyy-MM-dd HH:mm:ss )
	 *
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static Date StringToDate3(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = (Date) sdf.parse(str);
		return date;
	}

	/**
	 * 字符串转date(字符串格式:yyyy年MM月dd日  HH:mm)2020年01月03日  15:00
	 *
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static Date StringToDate4(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
		Date date = (Date) sdf.parse(str);
		return date;
	}

	/**
	 * 字符串传date(字符串格式:EEE, dd MMM yyyy HH:mm a )
	 *
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static Date StringToDate5(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm a", Locale.US);
		Date date = (Date) sdf.parse(str);
		return date;
	}

	/**
	 * 字符串传date(字符串格式:MMM d, yyyy hh:mm a )
	 * March 4, 2020 6:00 AM
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static Date StringToDate6(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy hh:mm a", Locale.US);
		Date date = (Date) sdf.parse(str);
		return date;
	}

	/**
	 * 字符串转Calendar(字符串格式:yyyy-MM-dd)
	 *
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static Calendar StringToCalendar(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(str);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	// -----------------------------------------------------date转String----------------------------------------//
	/**
	 * 时间戳转字符串(转后的字符串格式:yyyy-mm-dd)
	 *
	 * @param ts
	 * @return
	 */
	public static String timestampToString(Timestamp ts) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(ts);
		return time;
	}

	/**
	 * yyyyMMdd 转 yyyy-MM-dd
	 *
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String stringToDateString(String str) throws Exception {
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
		String sfstr = sf2.format(sf1.parse(str));
		return sfstr;
	}

	/**
	 * date转字符串(yyyy-MM-dd HH:mm:ss)
	 *
	 * @param date 日期
	 * @return
	 */
	public static String dateToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	/**
	 * date转字符串(yyyy-MM-dd)
	 *
	 * @param date
	 * @return
	 */
	public static String dateToString2(Date date) {
		String time = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			time = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	/**
	 * date转换字符串(格式:yyyyMMdd)
	 *
	 * @param date
	 * @return
	 */
	public static String dateToString3(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String time = sdf.format(date);
		return time;
	}

	/**
	 * date转换字符串(转后字符格式:yyyyMMddHHmmss)
	 *
	 * @param date
	 * @return
	 */
	public static String dateToString4(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String time = sdf.format(date);
		return time;
	}

	/**
	 * date转字符串(yyyy-MM-dd HH:mm)
	 *
	 * @param date 日期
	 * @return
	 */
	public static String dateToString5(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(date);
	}

	// ------------------------------------------------日期格式转换------------------------------------------------------------
	/**
	 * 日期字符格式转换 mm/dd/yyyy--->yyyy-MM-dd
	 */
	@SuppressWarnings("deprecation")
	public static String transDateString(String str) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date(str);
		String string = df.format(date);
		return string;
	}

	/**
	 * 日期字符格式转换yyyy-MM-DD HH:mm:ss-->MM/dd/yyyy HH:mm:ss
	 *
	 * @return
	 *
	 * @throws Exception
	 */
	public static String transDateString2(String str) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = sdf.parse(str);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DATE);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		int second = calendar.get(Calendar.SECOND);

		String string = addNum(month) + "/" + addNum(day) + "/" + year + " " + addNum(hour) + ":" + addNum(minute) + ":"
				+ addNum(second);

		return string;

	}

	public static String addNum(int i) {
		String str = "";
		if ((i + "").length() == 1) {
			str = "0" + i;
		} else {
			str = i + "";
		}
		return str;
	}

	public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
			"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
			"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
			"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z" };

	/**
	 * 生成8位随机字符串
	 * @brief
	 * @param
	 * @return
	 *
	 */
	public static String generateShortUuid() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();
	}

	/**
	 * 获取某月最后一天
	 * @param searchTime
	 * @return
	 */
	public static String getLastDayOfMonth(String searchTime) throws Exception{
		//格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(searchTime);
		Calendar calendar = Calendar.getInstance();//日历对象
		calendar.setTime(date);//设置当前日期
		//获取某月最大天数
		int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		//设置日历中月份的最大天数
		calendar.set(Calendar.DAY_OF_MONTH, lastDay);
		String lastDayOfMonth = sdf.format(calendar.getTime());
		return lastDayOfMonth;
	}

	/**
	 * 获取某个月的第一天
	 * @param searchTime
	 * @return
	 */
	public static String getFirstDayOfMonth(String searchTime) throws Exception{
		//格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(searchTime);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		//获取某月最小天数
		int lastDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		//设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);

		String lastDayOfMonth = sdf.format(cal.getTime());
		return lastDayOfMonth;
	}

	/**
	 * 计算某天n天之后的日期
	 * @param day
	 * @return
	 */
	public static String getBeforeAfterDateDay(String time, int day) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(time);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		int Year = cal.get(Calendar.YEAR);
		int Month = cal.get(Calendar.MONTH);
		int Day = cal.get(Calendar.DAY_OF_MONTH);

		int NewDay = Day + day;

		cal.set(Calendar.YEAR, Year);
		cal.set(Calendar.MONTH, Month);
		cal.set(Calendar.DAY_OF_MONTH, NewDay);

		String newTime = sdf.format(new Date(cal.getTimeInMillis()));
		return newTime;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值