TimesUtils.java 2.94 KB
package com.ectrip.cyt.utils;


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

/**
 * Created by dc on 2018/6/28.
 * 时间工具类
 */

public class TimesUtils {

    private static long lastTime = 0;

    public static boolean over1s(long overTime) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - lastTime < overTime) {
            return true;
        }
        lastTime = nowTime;
        return false;
    }

    private static long lastSountTime = 0;

    public static boolean overTimes(long overTime) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - lastSountTime > overTime) {
            lastSountTime = nowTime;
            return true;
        }
        return false;
    }

    /**
     * 最后一次扫描到人脸的时间
     */
    private static long lastScanTime = 0;

    public static void setLastScanTime() {
        lastScanTime = System.currentTimeMillis();
    }

    public static boolean overScanTimes(long overTime) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - lastScanTime > overTime) {
            lastScanTime = nowTime;
            return true;
        }
        return false;
    }

    private static long lastFaceTime = 0;

    public static boolean overFaceTimes(long overTime) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - lastFaceTime > overTime) {
            lastFaceTime = nowTime;
            return true;
        }
        return false;
    }

    public static int getAge(String scanTicketId) {
        try {
            String year = scanTicketId.substring(6, 10);
            String mony = scanTicketId.substring(10, 12);
            String day = scanTicketId.substring(12, 14);
            String brithday = year + "-" + mony + "-" + day;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return getAgeth(sdf.parse(brithday));
        } catch (Exception e) {
            LogUtil.d("timeUtils", e.getMessage());
        }
        return 0;
    }

    //由出生日期获得年龄
    public static int getAgeth(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            } else {
                age--;
            }
        }
        return age;
    }

}