hive udf 中 实现周岁算法,并对入参日期做判定

本文介绍了一个Hive UDF函数,用于精确计算基于生日和结束日期的年龄,包括闰年和平年的日期验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.ebscn.cdh.udfs;
import org.apache.arrow.flatbuf.Int;
import org.apache.hadoop.hive.ql.exec.UDF;
import sun.rmi.runtime.Log;

import java.io.*;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class GetGovYears extends UDF {

    public static int evaluate(String birthday,String endDate) {

        // 当前时间
        Calendar curr = Calendar.getInstance();
        // 生日
        Calendar born = Calendar.getInstance();
        Date date = null;



        String temp=null;
        if (Integer.parseInt(birthday) > Integer.parseInt(endDate)){
            temp=birthday;
            birthday=endDate;
            endDate=temp;
        }

        for (int i = birthday.length();--i>=0;){
            if (!Character.isDigit(birthday.charAt(i))){
                return -1;
            }
        }
        for (int i = endDate.length();--i>=0;){
            if (!Character.isDigit(endDate.charAt(i))){
                return -1;
            }
        }

        if (birthday.length()!=8){
            return -1;
        }
        if (endDate.length()!=8){
            return -1 ;
        }

        if ( Integer.parseInt( birthday.substring(4,6)) > 12 || Integer.parseInt( endDate.substring(4,6)) > 12  ){
            return -1;
        }
        if (Integer.parseInt( birthday.substring(4,6)) ==1 ||
                Integer.parseInt( birthday.substring(4,6)) ==3 ||
                Integer.parseInt( birthday.substring(4,6)) ==5 ||
                Integer.parseInt( birthday.substring(4,6)) ==7 ||
                Integer.parseInt( birthday.substring(4,6)) ==8 ||
                Integer.parseInt( birthday.substring(4,6)) ==10 ||
                Integer.parseInt( birthday.substring(4,6)) ==12 ||
                Integer.parseInt( endDate.substring(4,6)) ==1 ||
                Integer.parseInt( endDate.substring(4,6)) ==3 ||
                Integer.parseInt( endDate.substring(4,6)) ==5 ||
                Integer.parseInt( endDate.substring(4,6)) ==7 ||
                Integer.parseInt( endDate.substring(4,6)) ==8 ||
                Integer.parseInt( endDate.substring(4,6)) ==10 ||
                Integer.parseInt( endDate.substring(4,6)) ==12){
            if (Integer.parseInt( endDate.substring(6,8)) > 31 || Integer.parseInt( birthday.substring(6,8)) > 31 ){
                return -1;
            }
        }
        if (Integer.parseInt( birthday.substring(4,6)) ==4 ||
                Integer.parseInt( birthday.substring(4,6)) ==6 ||
                Integer.parseInt( birthday.substring(4,6)) ==9 ||
                Integer.parseInt( birthday.substring(4,6)) ==11 ||
                Integer.parseInt( endDate.substring(4,6)) ==4 ||
                Integer.parseInt( endDate.substring(4,6)) ==6 ||
                Integer.parseInt( endDate.substring(4,6)) ==9 ||
                Integer.parseInt( endDate.substring(4,6)) ==11 ){
            if (Integer.parseInt( endDate.substring(6,8)) > 30 || Integer.parseInt( birthday.substring(6,8)) > 30 ){
                return -1;
            }

        }

        if (Integer.parseInt( birthday.substring(4,6)) ==2 &&  Integer.parseInt( birthday.substring(2,4)) ==0  ) {
            if (Integer.parseInt(birthday.substring(0, 4)) % 400 == 0){
                if (Integer.parseInt(birthday.substring(6, 8)) > 29){
                    return -1;
                }
            }else {
                if (Integer.parseInt(birthday.substring(6, 8)) > 28) {
                    return -1;
                }
            }

        }

        if (Integer.parseInt( endDate.substring(4,6)) ==2 &&  Integer.parseInt( endDate.substring(2,4)) ==0  ) {
            if (Integer.parseInt(endDate.substring(0, 4)) % 400 == 0){
                if (Integer.parseInt(endDate.substring(6, 8)) > 29){
                    return -1;
                }
            }else {
                if (Integer.parseInt(endDate.substring(6, 8)) > 28) {
                    return -1;
                }
            }

        }

        if (Integer.parseInt( birthday.substring(4,6)) ==2
                &&  Integer.parseInt( birthday.substring(2,4)) !=0
        ) {

            if (Integer.parseInt(birthday.substring(0, 4)) % 4 == 0 && Integer.parseInt(birthday.substring(0, 4)) % 100 != 0) {
                if (Integer.parseInt(birthday.substring(6, 8)) > 29) {
                    return -1;
                }
            }
            else {

                if (Integer.parseInt(birthday.substring(6, 8)) > 28) {
                    return -1;
                }
            }
        }

        if (Integer.parseInt( endDate.substring(4,6)) ==2
                &&  Integer.parseInt( endDate.substring(2,4)) !=0
        ) {

            if (Integer.parseInt(endDate.substring(0, 4)) % 4 == 0 && Integer.parseInt(endDate.substring(0, 4)) % 100 != 0) {
                if (Integer.parseInt(endDate.substring(6, 8)) > 29) {
                    return -1;
                }
            }
            else {

                if (Integer.parseInt(endDate.substring(6, 8)) > 28) {
                    return -1;
                }
            }
        }





        try {
            date = new SimpleDateFormat("yyyyMMdd").parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date_end = null;
        try {
            date_end = new SimpleDateFormat("yyyyMMdd").parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        born.setTime(date);
        curr.setTime(date_end);
        // 年龄 = 当前年 - 出生年



//        if (curr.get(Calendar.YEAR) - born.get(Calendar.YEAR)<0){
//
//        }

        int age = curr.get(Calendar.YEAR) - born.get(Calendar.YEAR);
        if (age == 0) {
            return 0;
        }
        if (age < 0) {
            return -1;
        }
        // 如果当前月份小于出生月份: age-1
        // 如果当前月份等于出生月份, 且当前日小于出生日: age-1



        int currMonth = curr.get(Calendar.MONTH);


        int currDay = curr.get(Calendar.DAY_OF_MONTH);




        int bornMonth = born.get(Calendar.MONTH);

        int bornDay = born.get(Calendar.DAY_OF_MONTH);

        if ((currMonth < bornMonth) || (currMonth == bornMonth && currDay <= bornDay)) {
            age--;
        }
        return age < 0 ? 0 : age;
    }


    public static void main(String[] args) {
        GetGovYears t1=new GetGovYears();
        System.out.println( evaluate("19950228","20200229" ));
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值