java停车收费计算工具类

java

浏览数:509

2019-1-7

停车收费计算工具类.收费标准:8时至22时,停车1小时内3元,超过1小时每延长1小时加收3元;11时至12时、16时30分至17时30分,停车30分钟内收1元,超过30分收3元;晚22时至次日8时,收10元。


ParkingFeeUtil.java

package cn.dongqihong;

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

/**
 * 停车收费计算工具类<br/>
 * 收费标准:<br/>
 * 8时至22时,停车1小时内3元,超过1小时每延长1小时加收3元;<br/>
 * 11时至12时、16时30分至17时30分,停车30分钟内收1元,超过30分收3元;<br/>
 * 晚22时至次日8时,收10元。<br/>
 * 
 * 
 * @author DongQihong
 *
 */
public class ParkingFeeUtil {
  /** 一天的毫秒数 */
    public static final long ONE_DAY_MILLIS = 1 * 24 * 60 * 60 * 1000;
    /** 一小时的毫秒数 */
    public static final long ONE_HOUR_MILLIS = 1 * 60 * 60 * 1000;
    /** 半小时的毫秒数 */
    public static final long HALF_HOUR_MILLIS = 30 * 60 * 1000;

    /**
     * 计算停车费
     * 
     * @param time1
     *            驶入时间
     * @param time2
     *            驶出时间
     * @return 应收停车费
     */
    public static int countParkingFee(Date time1, Date time2) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time1);

        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        if (time1.before(calendar.getTime())) {// 8点前驶入
            return countNightParkingFee(time1, time2);
        } else {// 8点后驶入
            calendar.set(Calendar.HOUR_OF_DAY, 22);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            if (time1.before(calendar.getTime())) {// 22点前驶入
                return countNormalParkingFee(time1, time2);
            } else {// 22点后驶入
                return countNightParkingFee(time1, time2);
            }
        }

    }

    /**
     * 计算正常停车费<br/>
     * 8时至22时,停车1小时内3元,超过1小时每延长1小时加收3元;<br/>
     * 11时至12时、16时30分至17时30分,停车30分钟内收1元,超过30分收3元;<br/>
     * 
     * @param time1
     *            驶入时间
     * @param time2
     *            驶出时间
     * @return 应收停车费
     */
    private static int countNormalParkingFee(Date time1, Date time2) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time1);

        calendar.set(Calendar.HOUR_OF_DAY, 11);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        // 在11点后驶入
        if (!time1.before(calendar.getTime())) {
            calendar.set(Calendar.HOUR_OF_DAY, 12);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            if (time2.before(calendar.getTime())) {// 在12点前驶出
                long interval = (time2.getTime() - time1.getTime())
                        / HALF_HOUR_MILLIS;
                long mod = (time2.getTime() - time1.getTime())
                        % HALF_HOUR_MILLIS;
                if (mod > 0) {
                    interval = interval + 1;
                }
                if (interval > 1) {// 大于半小时
                    return 3;
                } else {// 半小时内
                    return 1;
                }
            }
        }

        calendar.set(Calendar.HOUR_OF_DAY, 16);
        calendar.set(Calendar.MINUTE, 30);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        // 在16点30后驶入
        if (!time1.before(calendar.getTime())) {
            calendar.set(Calendar.HOUR_OF_DAY, 17);
            calendar.set(Calendar.MINUTE, 30);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            if (time2.before(calendar.getTime())) {// 17点30前驶出
                long interval = (time2.getTime() - time1.getTime())
                        / HALF_HOUR_MILLIS;
                long mod = (time2.getTime() - time1.getTime())
                        % HALF_HOUR_MILLIS;
                if (mod > 0) {
                    interval = interval + 1;
                }
                if (interval > 1) {// 大于半小时
                    return 3;
                } else {// 半小时内
                    return 1;
                }
            }
        }

        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 在8点前驶入
        if (time1.before(calendar.getTime())) {
            return countNightParkingFee(time1, time2);
        } else {// 进入时间在8点后
            calendar.set(Calendar.HOUR_OF_DAY, 22);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);

            // 22点前驶出
            if (time2.before(calendar.getTime())) {
                long interval = (time2.getTime() - time1.getTime())
                        / ONE_HOUR_MILLIS;
                long mod = (time2.getTime() - time1.getTime())
                        % ONE_HOUR_MILLIS;
                if (mod > 0) {
                    interval = interval + 1;
                }
                return (int) (3 * interval);
            } else {// 超过22点
                long interval = (calendar.getTimeInMillis() - time1.getTime())
                        / ONE_HOUR_MILLIS;
                long mod = (calendar.getTimeInMillis() - time1.getTime())
                        % ONE_HOUR_MILLIS;
                if (mod > 0) {
                    interval = interval + 1;
                }
                return (int) (3 * interval)
                        + countNightParkingFee(calendar.getTime(), time2);
            }
        }
    }

    /**
     * 计算深夜停车费<br/>
     * 晚22时至次日8时,收10元。<br/>
     * 
     * @param time1
     *            驶入时间
     * @param time2
     *            驶出时间
     * @return 应收停车费
     */
    private static int countNightParkingFee(Date time1, Date time2) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time1);

        calendar.set(Calendar.HOUR_OF_DAY, 22);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 在22点前驶入
        if (time1.before(calendar.getTime())) {
            return countNormalParkingFee(time1, time2);
        } else {// 在22点后驶入
            calendar.add(Calendar.DATE, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 8);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            // 在次天8天前离开
            if (time2.before(calendar.getTime())) {
                return 10;
            } else {
                long interval = (time2.getTime() - time1.getTime())
                        / ONE_HOUR_MILLIS;
                long mod = (time2.getTime() - time1.getTime())
                        % ONE_HOUR_MILLIS;
                if (mod > 0) {
                    interval = interval + 1;
                }
                return 10 + countNormalParkingFee(calendar.getTime(), time2);
            }
        }
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time1 = null;
        Date time2 = null;

        //1
        time1 = format.parse("2015-10-01 11:36:23");
        time2 = format.parse("2015-10-01 11:36:30");
        System.out.println(countParkingFee(time1, time2));
        
        //18
        time1 = format.parse("2015-10-01 08:00:00");
        time2 = format.parse("2015-10-01 13:36:30");
        System.out.println(countParkingFee(time1, time2));
        
        //42
        time1 = format.parse("2015-10-01 08:00:00");
        time2 = format.parse("2015-10-01 21:59:59");
        System.out.println(countParkingFee(time1, time2));
        
        //52
        time1 = format.parse("2015-10-01 08:00:00");
        time2 = format.parse("2015-10-01 22:00:00");
        System.out.println(countParkingFee(time1, time2));
        
        //16
        time1 = format.parse("2015-10-01 21:36:23");
        time2 = format.parse("2015-10-02 08:36:30");
        System.out.println(countParkingFee(time1, time2));
        
        //10
        time1 = format.parse("2015-10-01 22:36:23");
        time2 = format.parse("2015-10-02 06:36:30");
        System.out.println(countParkingFee(time1, time2));
        
        //12
        time1 = format.parse("2015-10-01 10:36:23");
        time2 = format.parse("2015-10-01 13:36:30");
        System.out.println(countParkingFee(time1, time2));
        
        //6
        time1 = format.parse("2015-10-01 10:36:23");
        time2 = format.parse("2015-10-01 11:36:30");
        System.out.println(countParkingFee(time1, time2));
        
        //3
        time1 = format.parse("2015-10-01 10:36:23");
        time2 = format.parse("2015-10-01 11:26:30");
        System.out.println(countParkingFee(time1, time2));
        
        //6
        time1 = format.parse("2015-10-01 11:16:23");
        time2 = format.parse("2015-10-01 12:26:30");
        System.out.println(countParkingFee(time1, time2));
        
        //3
        time1 = format.parse("2015-10-01 11:00:00");
        time2 = format.parse("2015-10-01 12:00:00");
        System.out.println(countParkingFee(time1, time2));
        
        //0
        time1 = format.parse("2015-10-01 08:00:00");
        time2 = format.parse("2015-10-01 08:00:00");
        System.out.println(countParkingFee(time1, time2));
        
        //52
        time1 = format.parse("2015-10-01 08:00:00");
        time2 = format.parse("2015-10-02 08:00:00");
        System.out.println(countParkingFee(time1, time2));
        
        //58
        time1 = format.parse("2015-10-01 08:00:00");
        time2 = format.parse("2015-10-02 10:00:00");
        System.out.println(countParkingFee(time1, time2));
        
        time1 = format.parse("2015-10-01 08:00:00");
        time2 = format.parse("2016-10-01 08:00:00");
        System.out.println(countParkingFee(time1, time2));
    }
}