Java Date type time operation, subtracting the year to calculate the seniority score

/**
     * 工龄折算分数
     * 1.1.1小学:记60分结合折算0.54分
     * 1.1.2初中:记70分结合折算0.63分
     * 1.1.3中专(高中):记80分结合折算0.72分
     * 1.1.4大专:记90分结合折算0.81分
     * 1.1.5本科(及以上):记100分结合折算0.9分
     * @param workYears
     * @return
     */
    public static double getWorkYearsScore(Date workYears) {
        SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");
        ParsePosition pos = new ParsePosition(0);
        Date startWorkYear = formatter.parse(formatter.format(workYears), pos);
        ParsePosition pos2 = new ParsePosition(0);
        Date nowDate = formatter.parse(formatter.format(new Date()), pos2);
        //将Date类型转化为LocalDate
        ZoneId zoneId = ZoneId.systemDefault();
        Instant instant = startWorkYear.toInstant();
        LocalDate localstartWorkYear = instant.atZone(zoneId).toLocalDate();
        Instant instant2 = nowDate.toInstant();
        LocalDate localnowDate = instant2.atZone(zoneId).toLocalDate();
        Period period = Period.between(localstartWorkYear, localnowDate);
        int years = period.getYears();
        double score;
        if (years >= 0 && years <= 3) {
            score = 0.3;
        } else if (years >= 4 && years <= 6) {
            score = 0.35;
        } else if (years >= 7 && years <= 10) {
            score = 0.4;
        } else if (years >= 11 && years <= 20) {
            score = 0.45;
        } else {
            score = 0.5;
        }
        return score;
    }

Guess you like

Origin blog.csdn.net/weixin_39709134/article/details/126638952