Java结合Kotlin实现宝宝年龄计算

2022-11-13 11:11:12 计算 年龄 宝宝

通过生日计算宝宝多大了

前段时间大家在打新冠状疫苗的时候,护士给了我一张小纸条,上面清晰的显示我在地球上的年龄26岁4个月零3天。我们也写个代码走起

import java.text.ParseException;
import java.text.SimpleDateFORMat;
import java.util.Calendar;
public class LifeAge{
    
    public static String getAgeByBirth(String birthday) {
        //从Calendar对象中或得一个Date对象
        Calendar now = Calendar.getInstance();
        
        Calendar bir = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            bir.setTime(simpleDateFormat.parse(birthday));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //如果生日大于当前日期,则抛出异常:出生日期不能大于当前日期
        if (now.before(birthday)) {
            throw new IllegalArgumentException("The birthday is before Now,It's unbelievable");
        }
        //取出当前年月日
        int yearNow = now.get(Calendar.YEAR);
        int monthNow = now.get(Calendar.MONTH) + 1;
        int dayNow = now.get(Calendar.DAY_OF_MONTH);
        //取出出生年月日
        int yearBirth = bir.get(Calendar.YEAR);
        int monthBirth = bir.get(Calendar.MONTH) + 1;
        int dayBirth = bir.get(Calendar.DAY_OF_MONTH);
        //先day相减,不够向month借,然后month相减,不够向year借,最后year相减。
        int day = dayNow - dayBirth;
        int month = monthNow - monthBirth;
        //年龄是当前年减去出生年
        int year = yearNow - yearBirth;
        if (day < 0) {
            month -= 1;
            now.add(Calendar.MONTH, -1);//得到上一个月,用来得到上个月的天数。
            //借一个月
            day = day + now.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        if (month < 0) {
            //一年12个月,借12个月
            month = (month + 12) % 12;
            //借了12个月,少一年
            year--;
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(year + "岁");
        stringBuilder.append(month + "月");
        if (day < 20) {
            stringBuilder.append("零" + day + "天");
        } else {
            stringBuilder.append(day + "天");
        }
        if (year == 0 && month == 0 && day == 0) {
            stringBuilder.delete(0, stringBuilder.length());
            stringBuilder.append("今日出生");
        }
        return stringBuilder.toString();
    }
    
    public static void main(String[] args) {
        String birthday = "1993-10-18";
        String age = getAgeByBirth(birthday);
        System.out.print("年龄=" + age);
    }
}

输出结果

26岁7月零9天

再来一个Kotlin版

import java.text.SimpleDateFormat
import java.util.*
class LifeAge {
    
     fun getAgeByBirth(birthday: String?): String {
        //从Calendar对象中或得一个Date对象
        val now = Calendar.getInstance()
        
        val bir = Calendar.getInstance()
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
        bir.time = simpleDateFormat.parse(birthday)
        //如果生日大于当前日期,则抛出异常:出生日期不能大于当前日期
        require(!now.before(birthday)) { "The birthday is before Now,It's unbelievable" }
        //取出当前年月日
        val yearNow = now[Calendar.YEAR]
        val monthNow = now[Calendar.MONTH] + 1
        val dayNow = now[Calendar.DAY_OF_MONTH]
        //取出出生年月日
        val yearBirth = bir[Calendar.YEAR]
        val monthBirth = bir[Calendar.MONTH] + 1
        val dayBirth = bir[Calendar.DAY_OF_MONTH]
        //先day相减,不够向month借,然后month相减,不够向year借,最后year相减。
        var day = dayNow - dayBirth
        var month = monthNow - monthBirth
        //年龄是当前年减去出生年
        var year = yearNow - yearBirth
        if (day < 0) {
            month -= 1
            now.add(Calendar.MONTH, -1) //得到上一个月,用来得到上个月的天数。
            //借一个月
            day += now.getActualMaximum(Calendar.DAY_OF_MONTH)
        }
        if (month < 0) {
            //一年12个月,借12个月
            month = (month + 12) % 12
            //借了12个月,少一年
            year--
        }
        val stringBuilder = StringBuilder()
        stringBuilder.append(year.toString() + "岁")
        stringBuilder.append(month.toString() + "月")
        if (day < 20) {
            stringBuilder.append("零" + day + "天")
        } else {
            stringBuilder.append(day.toString() + "天")
        }
        if (year == 0 && month == 0 && day == 0) {
            stringBuilder.delete(0, stringBuilder.length)
            stringBuilder.append("今日出生")
        }
        return stringBuilder.toString()
    }
}

fun main() {
    val birthday = "1994-10-18"
    val age = LifeAge().getAgeByBirth(birthday)
    print("年龄=$age")
}

输出结果

以上就是Java结合Kotlin实现宝宝年龄计算的详细内容,更多关于Java Kotlin年龄计算的资料请关注其它相关文章!

相关文章