通过生日获取年龄+生肖+星座工具类

简介: 通过生日获取年龄+生肖+星座工具类

通过生日获取年龄+生肖+星座工具类

BirthdayUtil:


package com.tjcu.utils;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class BirthdayUtil {
    //根据生日计算生肖,年龄,星座
    private final static int[] dayArr = new int[] { 20, 19, 21, 20, 21, 22, 23,
            23, 23, 24, 23, 22 };
    private final static ArrayList<String> constellationList = new ArrayList<>();//存放星座的集合
     static {
         constellationList.add(0, "水瓶座");
         constellationList.add(1, "双鱼座");
         constellationList.add(2, "白羊座");
         constellationList.add(3, "金牛座");
         constellationList.add(4, "双子座");
         constellationList.add(5, "巨蟹座");
         constellationList.add(6, "狮子座");
         constellationList.add(7, "处女座");
         constellationList.add(8, "天秤座");
         constellationList.add(9, "天蝎座");
         constellationList.add(10, "射手座");
         constellationList.add(11, "魔羯座");
     }
    //获得年龄
    public static Integer  getAge(Date birthday){
        int year1 = birthday.getYear();
        Date date = new Date();
        int year2 = date.getYear();
        return  year2-year1;
    }
    //获得生肖
    public static String getChineseZodiac(Date birthday) {
        int year = birthday.getYear();
        //0代表1900年
        if (year < 0) {
            return "未知";
        }
        int start = 0;
        String[] years = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊",
                "猴", "鸡", "狗", "猪" };
        return years[(year - start) % 12];
    }
    //获得星座
    /**
     * 传入日期,返回星座
     */
    public static String getConstellation(Date date) {
        String constellation = "";
        Calendar birthday = Calendar.getInstance();
        birthday.setTime(date);
        int month = birthday.get(Calendar.MONTH) + 1;
        int day = birthday.get(Calendar.DAY_OF_MONTH);
        switch (month) {
            case 1:
                //Capricorn 摩羯座(12月22日~1月20日)
                constellation = day <= 20 ? constellationList.get(11) : constellationList.get(0);
                break;
            case 2:
                //Aquarius 水瓶座(1月21日~2月19日)
                constellation = day <= 19 ? constellationList.get(0) : constellationList.get(1);
                break;
            case 3:
                //Pisces 双鱼座(2月20日~3月20日)
                constellation = day <= 20 ? constellationList.get(1) : constellationList.get(2);
                break;
            case 4:
                //白羊座 3月21日~4月20日
                constellation = day <= 20 ? constellationList.get(2) : constellationList.get(3);
                break;
            case 5:
                //金牛座 4月21~5月21日
                constellation = day <= 21 ? constellationList.get(3) : constellationList.get(4);
                break;
            case 6:
                //双子座 5月22日~6月21日
                constellation = day <= 21 ? constellationList.get(4) : constellationList.get(5);
                break;
            case 7:
                //Cancer 巨蟹座(6月22日~7月22日)
                constellation = day <= 22 ? constellationList.get(5) : constellationList.get(6);
                break;
            case 8:
                //Leo 狮子座(7月23日~8月23日)
                constellation = day <= 23 ? constellationList.get(6) : constellationList.get(7);
                break;
            case 9:
                //Virgo 处女座(8月24日~9月23日)
                constellation = day <= 23 ? constellationList.get(7) : constellationList.get(8);
                break;
            case 10:
                //Libra 天秤座(9月24日~10月23日)
                constellation = day <= 23 ? constellationList.get(8) : constellationList.get(9);
                break;
            case 11:
                //Scorpio 天蝎座(10月24日~11月22日)
                constellation = day <= 22 ? constellationList.get(9) : constellationList.get(10);
                break;
            case 12:
                //Sagittarius 射手座(11月23日~12月21日)
                constellation = day <= 21 ? constellationList.get(10) : constellationList.get(11);
                break;
        }
        return constellation;
    }
}

测试:


 student.setSbirthday(new Date());
        Date birthday = student.getSbirthday();
        //获取年龄
        Integer age = BirthdayUtil.getAge(birthday);
        //获取生肖
        String attr = BirthdayUtil.getChineseZodiac(birthday);
        //获取星座
        String star = BirthdayUtil.getConstellation(birthday);
        System.out.println("生日:"+birthday);
        System.out.println("年龄:"+attr);
        System.out.println("生肖:"+attr);
        System.out.println("星座:"+star);

效果图:

image.png

相关文章
|
NoSQL Linux 测试技术
Linux下Redis的安装、配置及开机自启动
Linux下Redis的安装、配置及开机自启动 系统版本: CentOS 7 Redis版本: Redis-6.2.5
46681 7
Linux下Redis的安装、配置及开机自启动
Mac下查看公网以及内网IP地址
Mac下查看公网以及内网IP地址
589 0
Java实现多文件打包成压缩包下载
Java实现多文件打包成压缩包下载
723 0
阿里云实名认证api接口怎么调用
当我们注册一个购物网站,或者下载某个游戏,很多地方都需要做实名认证。那么作为购物网站,或者游戏公司,怎么才能判断客户提供的身份证号码是否真实呢?游戏玩家越来越多,我们可能人工去审核这个人提供个的身份证号码是否属实,或者是否是真人。根据国家规定,我们很多游戏都要对未成年游戏时长进行控制,也就是通常大家所谓的游戏防沉迷系统。我们只要把那些进行实名认证玩家的年龄给摘出来以后,就可以判断其是否成年。
阿里云实名认证api接口怎么调用
|
11月前
|
Java Maven
震惊!idea专业版如何配置maven国内源手把手教学
文章提供了如何在IDEA专业版中配置Maven使用国内源(如阿里云)的详细步骤,以加快依赖下载速度,并解释了配置国内源的原因。
2613 0
震惊!idea专业版如何配置maven国内源手把手教学
|
JavaScript 前端开发 API
autoxjs的介绍
autoxjs的介绍
|
数据采集 网络协议 物联网
C# | 上位机开发新手指南(一)概述
C#,是微软主推的编程语言。它在工业控制、自动化、物联网等领域应用非常广泛。由于国内在工业控制领域技术发展路径的原因,早期的自动化控制面板由Window环境提供,大量的MFC、VB6控制应用被部署在了工厂车间。在用户习惯和界面环境的双料加持下,给C#在工业领域的推广打下的坚实的基础。使用C#进行上位机开发已经是行业内的主流选择。 如果正在读文章的你准备或正在从事C#的上位机开发,本专栏将会是您在这条路线上的路标。欢迎一同打卡每一个节点。
2082 0
C# | 上位机开发新手指南(一)概述
|
编解码 算法 Serverless
m通过概率整形技术对1024QAM进行星座图整形,并输出GMI指标
m通过概率整形技术对1024QAM进行星座图整形,并输出GMI指标
530 0
|
Web App开发 IDE Java
手把手教你下载安装Goland 新手别错过!
手把手教你下载安装Goland 新手别错过!
675 0
|
机器学习/深度学习 传感器 运维
快速开发光伏电站数字孪生运维系统(一)
本文重点介绍如何从零开始构建出光伏电站数字孪生系统的详细步骤。
927 0
快速开发光伏电站数字孪生运维系统(一)

热门文章

最新文章