关于日期处理的工具类

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

/**
 * @ClassName DateUtil
 * @Description 关于日期的工具类
 * @author 大白能
 * @date 2016-12-30 上午10:57
 */
public class DateUtil {
    // 年月日
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_ZH = "yyyy年MM月dd日";
    // 时分秒
    public static final String HH_MM_SS = "HH:mm:ss";
    public static final String HH_MM_SS_ZH = "HH时mm分ss秒";
    // 年月日 时分秒
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM_SS_ZH = "yyyy年MM月dd日 HH时mm分ss秒";

    // 私有化构造函数,防止创建该工具类的对象
    private DateUtil() {
    }

    /**
     * @Title containPoint
     * @Description 判断一个时间点是否包含在一个时间段内
     * @param start
     *            开始时间字符串
     * @param end
     *            结束时间字符串
     * @param now
     *            需要判断的时间点字符串
     * @param format
     *            字符串格式
     * @return boolean 是否包含
     */
    public static boolean containPoint(String start, String end, String now,
            String format) throws ParseException {
        return containPoint(stringToDate(start, format),
                stringToDate(end, format), stringToDate(now, format));
    }

    /**
     * @Title containPoint
     * @Description 判断一个时间点是否包含在一个时间段内
     * @param start
     *            开始时间
     * @param end
     *            结束时间
     * @param now
     *            需要判断的时间点
     * @return boolean 是否包含
     */
    public static boolean containPoint(Date start, Date end, Date now) {
        // 判断开始时间是否在结束时间之前
        if (start.before(end)) {
            // 判断的时间点在开始时间之前或在结束时间之后即可
            if (now.before(start) || now.after(end)) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    /**
     * @Title dateToString
     * @Description 将日期转化为固定格式的字符串
     * @param date
     *            需要转化的日期
     * @param format
     *            需要格式成的字符串样式
     * @return String 转化后的字符串
     */
    public static String dateToString(Date date, String format) {
        // 格式化日期
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * @Title stringToDate
     * @Description 将字符串转化为日期类型
     * @param dateStr
     *            需要转化的字符串
     * @param format
     *            需要格式成的日期样式
     * @return Date 转化后的日期
     */
    public static Date stringToDate(String dateStr, String format)
            throws ParseException {
        // 返回格式化后的日期
        return new SimpleDateFormat(format).parse(dateStr);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值