DateUtils工具类

class DateUtils {

    private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();
    private static final Object lockObj = new Object();

    /**
     * 毫秒转天
     */
    public static int MILLISECOND_TURN_DAYS = 1000 * 3600 * 24;

    private static SimpleDateFormat getSdf(final String pattern) {
        ThreadLocal<SimpleDateFormat> simpleDateFormat = sdfMap.get(pattern);
        // 此处的双重判断和同步是为了防止sdfMap这个单例被多次put重复的sdf
        if (simpleDateFormat == null) {
            synchronized (lockObj) {
                simpleDateFormat = sdfMap.get(pattern);
                if (simpleDateFormat == null) {
                    simpleDateFormat = new ThreadLocal<SimpleDateFormat>() {
                        @Override
                        protected SimpleDateFormat initialValue() {
                            return new SimpleDateFormat(pattern);
                        }
                    };
                    sdfMap.put(pattern, simpleDateFormat);
                }
            }
        }
        return simpleDateFormat.get();
    }

    /**
     * 
     * @Title: format
     * @Description: 把日期格式化为指定格式
     * @param date
     *            要格式化的日期
     * @param pattern
     *            日期格式字符串
     * @return 格式化之后的日期字符串
     */
    public static String format(Date date, String pattern) {
        return getSdf(pattern).format(date);
    }
    /**
     * 
     * @Title: getContinuousDates
     * @Description: 获取连续的日期
     * @param startTime 
     *               开始日期
     * @param endTime
     *               结束日期
     * @return 日期列表
     */
    public static List<String> getContinuousDates(Date startTime, Date endTime) {
        int days = getDaysBetween(startTime, endTime);
        List<String> list = new ArrayList<String>();
        list.add(format(endTime, "yyyy-MM-dd"));
        Calendar cal = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(endTime);
        while (days > 0) {
            // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, -1);
            list.add(format(cal.getTime(), "yyyy-MM-dd"));
            days--;
        }
        return list;
    }

    /**
     * 
     * @Title: getDaysBetween
     * @Description: 获取两个时间的天数差
     * @param early
     *            早的时间
     * @param late
     *            晚的时间
     * @return 天数
     */
    public static int getDaysBetween(Date early, Date late) {
        Calendar calendarEarly = Calendar.getInstance();
        Calendar calendarLate = Calendar.getInstance();
        calendarEarly.setTime(early);
        calendarLate.setTime(late);
        // 设置时间为0时
        calendarEarly.set(Calendar.HOUR_OF_DAY, 0);
        calendarEarly.set(Calendar.MINUTE, 0);
        calendarEarly.set(Calendar.SECOND, 0);
        calendarLate.set(Calendar.HOUR_OF_DAY, 0);
        calendarLate.set(Calendar.MINUTE, 0);
        calendarLate.set(Calendar.SECOND, 0);
        // 得到两个日期相差的天数
        int number = (int) (calendarLate.getTime().getTime() / MILLISECOND_TURN_DAYS
                - calendarEarly.getTime().getTime() / MILLISECOND_TURN_DAYS);
        return number;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_30038111/article/details/79927177