一个时间转换的工具类

package com.founder.cissservice.util;

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

public class FormatDate {
	/**
	 * 根据日历时间提取yyyy-mm-dd格式的时间
	 * 
	 * @param Date
	 * @return String 2002-12-13 Format
	 */
	public static String toY_M_D(Date date) throws RuntimeException {
		return toSimpleDateFormat(date, "yyyy-MM-dd");
	}

	/**
	 * 根据日历时间提取yyyymmdd格式的时间
	 * 
	 * @param Date
	 * @return String 20021213 Format
	 */
	public static String toYMD(Date date) throws RuntimeException {
		return toSimpleDateFormat(date, "yyyyMMdd");
	}
	
	
	public static String toCN_Y_M_D(Date date) throws RuntimeException {
		return toSimpleDateFormat(date, "yyyy年MM月dd日");
	}

	/**
	 * 返回yyyy-mm-dd hh:mm:ss格式的时间
	 * 
	 * @param date
	 * @return
	 */
	public static String toY_M_D_H_M_S(Date dateTime) throws RuntimeException {
		return toSimpleDateFormat(dateTime, "yyyy-MM-dd HH:mm:ss");
	}

	public static String toCN_Y_M_D_H_M_S(Date dateTime) throws RuntimeException {
		return toSimpleDateFormat(dateTime, "yyyy年MM月dd日 HH时mm分ss秒");
	}

	/**
	 * 返回yyyy-mm-dd hh:mm格式的时间
	 * 
	 * @param date
	 * @return
	 */
	public static String toY_M_D_H_M(Date dateTime) throws RuntimeException {
		return toSimpleDateFormat(dateTime, "yyyy-MM-dd HH:mm");
	}

	public static String toCN_Y_M_D_H_M(Date dateTime) throws RuntimeException {
		return toSimpleDateFormat(dateTime, "yyyy年MM月dd日 HH时mm分");
	}

	/**
	 * 根据时间返回hh:mm:ss格式的字符串
	 * 
	 * @param time
	 * @return
	 * @throws Exception
	 */
	public static String toH_M_S(Date time) throws RuntimeException {
		if (time == null) {
			return null;
		} else {
			Calendar cd = Calendar.getInstance();
			cd.setTime(time);
			return cd.get(Calendar.HOUR_OF_DAY)+":"+cd.get(Calendar.MINUTE)+":"+cd.get(Calendar.SECOND);
//			return time.getHours() + ":" + time.getMinutes() + ":"
//					+ time.getSeconds();
		}
	}

	/**
	 * 字符串格式转换称日期格式
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date fromY_M_D(String date) throws RuntimeException {
		return fromSimpleDateFormat(date, "yyyy-MM-dd");
	}

	/**
	 * 日期时间格式
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date fromY_M_D_H_M_S(String dateTime) throws RuntimeException {
		return fromSimpleDateFormat(dateTime, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 根据时分秒装还称时间
	 * 
	 * @param time
	 * @return
	 * @throws Exception
	 */
	public static Date fromH_M_S(String time) throws RuntimeException {
		try {
			return fromSimpleDateFormat(time, "HH:mm:ss");
		} catch (Exception e) {
			return fromSimpleDateFormat(time, "yyyy-MM-dd HH:mm:ss");
		}
	}

	/**
	 * 得到当前的时间
	 * 
	 * @return
	 */
	public static Date getCurrDate() {
		return new Date();
	}

	/**
	 * 统一的时间构造方法
	 * 
	 * @param date
	 * @param simpleDateFormat
	 * @return
	 */
	public static String toSimpleDateFormat(Date date, String simpleDateFormat) {
		SimpleDateFormat format = new SimpleDateFormat(simpleDateFormat);
		if (date == null) {
			return null;
		} else {
			return format.format(date);
		}
	}

	/**
	 * 简单时间格式
	 * 
	 * @param date
	 * @param simpleDateFormat
	 * @return
	 */
	public static Date fromSimpleDateFormat(String date, String simpleDateFormat)
			throws RuntimeException {
		SimpleDateFormat format = new SimpleDateFormat(simpleDateFormat);
		if (date == null || date.equals("")) {
			return null;
		} else {
			try {
				return format.parse(date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值