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

简介: 通过生日获取年龄+生肖+星座工具类🍅 Java学习路线:搬砖工的Java学习路线🍅 作者:程序员小王🍅 程序员小王的博客:https://www.wolai.com/wnaghengjie/ahNwvAUPG2Hb1Sy7Z8waaF🍅 扫描主页左侧二维码,加我微信 一起学习、一起进步🍅 欢迎点赞 👍 收藏 ⭐留言 📝🍅 如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去其糟粕

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);


效果图:

2.png



相关文章
|
机器学习/深度学习 人工智能 算法
阿里通义最新黑科技!“通义舞王”:让静态照片翩翩起舞,探索艺术与科技的无限可能
【1月更文挑战第2天】在科技日新月异的时代,艺术创作的疆界正以前所未有的速度拓展,,从AI作曲和音乐生成技术带来的跨风格音乐作品,到基于人工智能的诗歌与文学创作,艺术不再仅仅是人类个体情感与才华的体现,而成为人机交互、数据智能与创新思维相互融合的新领域。 近日,阿里云再次引领创新潮流,推出一款令人叹为观止的AI黑科技——通义舞王
阿里通义最新黑科技!“通义舞王”:让静态照片翩翩起舞,探索艺术与科技的无限可能
10、pip换源加速下载的方式
10、pip换源加速下载的方式
10、pip换源加速下载的方式
|
9月前
|
人工智能 负载均衡 数据可视化
评测|零门槛,即刻拥有DeepSeek-R1满血版 | 阿里云百炼调用DeepSeek
本方案以 DeepSeek-R1 满血版为例进行演示,通过百炼模型服务进行 DeepSeek 开源模型调用,可以根据实际需求选择其他参数规模的 DeepSeek 模型。
199 0
|
4月前
|
存储 人工智能 安全
医学影像PACS系统的设计与实现,PACS源码
医学影像PACS系统是数字化医院的核心,实现影像的采集、存储、传输与辅助诊断。系统基于DICOM、HL7等标准,采用分布式架构与模块化设计,支持影像分层存储、高效调阅、AI集成及多系统联动,提升诊疗效率与数据管理能力。
768 0
|
传感器 物联网 网络虚拟化
物联网卡怎么取消区域限制
物联网卡(IoT SIM卡)通常用于连接物联网设备,如智能城市设备、工业传感器、车载设备等,以实现数据的远程传输和设备的远程管理。物联网卡通常会有区域限制,即只能在特定的地区或国家内使用。取消物联网卡的区域限制并不是一个直接的操作,因为这通常取决于服务提供商的政策和合同条款。不过,以下是一些可能的步骤和建议,可以尝试来解除或绕过这些限制:
|
运维 监控 数据可视化
大白话详解Spring Cloud服务降级与熔断
Hystrix断路器概述 1.1 分布式系统面临的问题 复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败。这就造成有可能会发生 服务雪崩 。那么什么是服务雪崩呢? 多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其他的微服务,这就是所谓的“ 扇出 ”(像一把打开的折扇)。如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,这就是所谓的” 雪崩效应 “。也就是系统的 高可用 受到了破坏。
|
运维 Linux
在Linux中,如何排查硬件故障?
在Linux中,如何排查硬件故障?
|
人工智能 开发框架 前端开发
适合假期自学一战成名的必看秘籍-五板斧打造AgentScope应用
本文写给有一定编程基础的学习者,得以入门 源码级 开发Agentscope应用,并上线创空间,参加AgentScope的应用开发挑战赛。
适合假期自学一战成名的必看秘籍-五板斧打造AgentScope应用
OpenFeign FormData
OpenFeign FormData
135 0
|
Java API
将`List<String>`转换为`List<Long>`
将`List<String>`转换为`List<Long>`
1175 0