Java对于时间的一些特殊处理函数

来源:互联网 发布:sql set case then 编辑:程序博客网 时间:2024/05/18 17:58
/**
     * 两端时间的小时差
     *
     * @param fromTime
     * @param toTime
     * @return
     */
    public static long calHoursFromDayToCurrentDay(String fromTime, String toTime) {
        long fromMillis = 0l;
        long toMillis = 0l;
        try {
            fromMillis = timeFormat.parse(fromTime).getTime();
            toMillis = timeFormat.parse(toTime).getTime();
        } catch (ParseException e) {
            throw new IllegalArgumentException();
        }
        long nd = 1000 * 24 * 3600;
        long nh = 1000 * 3600;
        // 获得两个时间的毫秒时间差异
        long diff = toMillis - fromMillis;
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh + day * 24;
        return hour;

    }


/**
     * 取出时间段内的dayInterval间隔的起始和结束时间,以#分割
     *
     * @param startTime
     * @param endTime
     * @param dayInterval
     * @return
     */
    public static List<String> getTimesIntervalList(String startReadTime, String endReadTime) {
        // SimpleDateFormat非线程安全,使用new实例化
        SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> allTimes = new ArrayList<>();
        String suffix_1 = " 00:00:00";
        String suffix_2 = " 23:59:59";
        int days = daysBetween(startReadTime, endReadTime, newFormat);
        if (days <= 0 || days <= dayInterval) {
            allTimes.add(startReadTime + "#" + endReadTime);
            return allTimes;
        }
        Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
        Date endDate = new Date();
        try {
            calendar.setTime(newFormat.parse(startReadTime));
            endDate = newFormat.parse(endReadTime);
        } catch (ParseException e) {
            throw new IllegalStateException();
        }
        while (calendar.getTime().compareTo(endDate) < 0) {
            String startTime = newFormat.format(calendar.getTime()) + suffix_1;
            calendar.add(Calendar.DATE, dayInterval - 1);
            String endTime;
            if (calendar.getTime().compareTo(endDate) > 0) {
                endTime = newFormat.format(endDate) + suffix_2;
            } else {
                endTime = newFormat.format(calendar.getTime()) + suffix_2;
            }
            String fromToEndTime = startTime + "#" + endTime;
            allTimes.add(fromToEndTime);
            calendar.add(Calendar.DATE, 1);
        }
        return allTimes;
    }

    /**
     * 两段日期的时间数量
     *
     * @param smdate
     * @param bdate
     * @return
     * @throws ParseException
     */
    public static int daysBetween(String smdate, String bdate, SimpleDateFormat sdf) {
        long between_days = 0l;
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(smdate));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            between_days = (time2 - time1) / (1000 * 3600 * 24);
        } catch (ParseException e) {
            throw new IllegalStateException();
        }
        return Integer.parseInt(String.valueOf(between_days));
    }



0 0
原创粉丝点击