java的常见时间格式转换函数

场景:
java的calendar,Date,Timestamp与字符串相互转换
1. 示例

/**
	 * 1.使用Calendar获取格式化的日期时间 根据格式化字符串获取时间
	 * */
	public static String getFormatDateTime(Calendar calendar, String format) {
		if (calendar != null) {

			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			return simpleDateFormat.format(calendar.getTime());
		}
		return null;
	}

	/**
	 * 2.使用Date获取格式化的日期时间 根据格式化字符串获取时间
	 * */
	public static String getFormatDateTime(java.util.Date date, String format) {
		if (date != null) {

			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			return simpleDateFormat.format(date.getTime());
		}
		return null;
	}

	/**
	 * 3.使用Date获取格式化的日期时间 根据格式化字符串获取时间
	 * */
	public static String getFormatDateTime(java.sql.Timestamp date,
			String format) {
		if (date != null) {

			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			return simpleDateFormat.format(date.getTime());
		}
		return null;
	}

	/**
	 * 4.将时间字符串按照格式转换为Timestamp时间
	 * */
	public static Timestamp strToTimestamp(String dateStr,
			String simpleDateFormat) {
		if (dateStr != null && !dateStr.trim().equals("")) {
			try {
				if (simpleDateFormat == null
						|| simpleDateFormat.trim().equals("")) {
					simpleDateFormat = "yyyy-MM-dd HH:mm:ss";
				}
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						simpleDateFormat);
				return new Timestamp(dateFormat.parse(dateStr).getTime());
			} catch (Exception e) {
				logger.error("", e);
			}
		}
		return null;
	}

	/**
	 * 5.将时间字符串按照格式转换为Date时间
	 * */
	public static java.util.Date strToDate(String dateStr,
			String simpleDateFormat) throws Exception {
		if (dateStr != null && !dateStr.trim().equals("")) {
			if (simpleDateFormat == null || simpleDateFormat.trim().equals("")) {
				simpleDateFormat = "yyyy-MM-dd HH:mm:ss";
			}
			SimpleDateFormat dateFormat = new SimpleDateFormat(simpleDateFormat);
			return dateFormat.parse(dateStr);
		}
		return null;
	}

	/**
	 * 6.根据具体指定时区获取Calendar "GMT+8"
	 * */
	public static Calendar getCalendarInstance(String timeZone) {
		Calendar cCreateTime = Calendar.getInstance();
		long needTimeRowOff = TimeZone.getTimeZone(timeZone).getRawOffset();
		long defaultTimeRowOff = TimeZone.getDefault().getRawOffset();
		if (needTimeRowOff != defaultTimeRowOff) {
			cCreateTime.setTimeZone(TimeZone.getTimeZone(timeZone));
		}
		return cCreateTime;
	}

	/**
	 * 7.根据指定时间字符串获取date
	 * */
	public static Date parseDate(String str) {
		if ((str == null) || (str.equals(""))) {
			return null;
		}
		Date dt = null;
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			dt = new Date(dateFormat.parse(str).getTime());
		} catch (ParseException e) {
			logger.error("", e);
		}
		return dt;
	}

	/**
	 * 8.时间字符串转换为date
	 * */
	public static Date parseDate(String str, String pattern) {
		if ((str == null) || (str.equals(""))) {
			return null;
		}
		Date date = null;
		DateFormat dateFormat = new SimpleDateFormat(pattern);
		try {
			date = new Date(dateFormat.parse(str).getTime());
		} catch (ParseException e) {
			logger.error("", e);
		}
		return date;
	}

	/** 9.将date转换为指定格式的时间 */
	public static String toString(Date date, String pattern) {
		if (date == null)
			return null;
		DateFormat dateFormat = new SimpleDateFormat(pattern);
		return dateFormat.format(date);
	}

	/** 10.将Timestamp转换为指定格式的时间 */
	public static String toString(Timestamp timestamp, String pattern) {
		if (timestamp == null)
			return null;
		DateFormat dateFormat = new SimpleDateFormat(pattern);
		return dateFormat.format(timestamp);
	}


以上,TKS.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值