关于日期处理的工具类

来源:互联网 发布:php高级工程师 编辑:程序博客网 时间:2024/06/06 03:29
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);    }}
0 0
原创粉丝点击