关于时间待处理的工具类

导读:本篇文章讲解 关于时间待处理的工具类,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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

/**
 * @ClassName DateUtil
 * @Description 关于日期的工具类
 * @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);
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118722.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!