java 根据生日计算星座

本文介绍了一个简单的星座查询工具的实现方式,通过输入生日日期可以自动计算并返回对应的星座名称。该工具使用Java编程语言,定义了十二星座的枚举类型,并实现了根据出生月份和日期判断星座的逻辑。
public class ConstellationUtil {

public enum Constellation {
Capricorn(1, "摩羯座"), Aquarius(2, "水瓶座"), Pisces(3, "双鱼座"), Aries(4,
"白羊座"), Taurus(5, "金牛座"), Gemini(6, "双子座"), Cancer(7, "巨蟹座"), Leo(
8, "狮子座"), Virgo(9, "处女座"), Libra(10, "天秤座"), Scorpio(11, "天蝎座"), Sagittarius(
12, "射手座");

private Constellation(int code, String chineseName) {
this.code = code;
this.chineseName = chineseName;
}
private int code;
private String chineseName;

public int getCode() {
return this.code;
}
public String getChineseName() {
return this.chineseName;
}
}

public static final Constellation[] constellationArr = {
Constellation.Aquarius, Constellation.Pisces, Constellation.Aries,
Constellation.Taurus, Constellation.Gemini, Constellation.Cancer,
Constellation.Leo, Constellation.Virgo, Constellation.Libra,
Constellation.Scorpio, Constellation.Sagittarius,
Constellation.Capricorn
};

public static final int[] constellationEdgeDay = { 21, 20, 21, 21, 22, 22,
23, 24, 24, 24, 23, 22 };

public static String calculateConstellation(String birthday) {
if (birthday == null || birthday.trim().length() == 0)
throw new IllegalArgumentException("the birthday can not be null");
String[] birthdayElements = birthday.split("-");
if (birthdayElements.length != 3)
throw new IllegalArgumentException(
"the birthday form is not invalid");
int month = Integer.parseInt(birthdayElements[1]);
int day = Integer.parseInt(birthdayElements[2]);
if (month == 0 || day == 0 || month > 12)
return "";
month = day < constellationEdgeDay[month - 1] ? month - 1:month;
return month > 0 ? constellationArr[month - 1].getChineseName(): constellationArr[11].getChineseName();
}
}
### 实现根据身份证号获取星座 通过解析中国居民身份证号码可以提取出生日期信息,进而计算对应的星座。以下是具体实现方法: #### 解析身份证号并获得出生日期 在中国大陆使用的18位身份证号码中,第7至14位代表持证人的出生年月日。因此可以通过截取这部分字符串来构建`Date`对象。 ```java import java.text.ParseException; import java.text.SimpleDateFormat; public class IDCardUtils { private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); public static String getBirthByIdCard(String idCard) throws ParseException { if (idCard.length() != 18 || !idCard.matches("\\d{17}[\\dxX]")) throw new IllegalArgumentException("Invalid ID card format"); // Extract birth date from the ID number String birthdayStr = idCard.substring(6, 14); return sdf.parse(birthdayStr).toString(); } } ``` #### 使用 Hutool 库中的 `DateUtil` 来确定星座 Hutool 提供了一个方便的方法可以直接传入月份和日子返回相应的星座名称[^1]。 ```java import cn.hutool.core.date.DateUtil; import java.util.Calendar; // Assuming we have already obtained the birthdate as a string like "yyyy-MM-dd" String zodiacSign = null; try { Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(getBirthByIdCard(idCard))); int month = cal.get(Calendar.MONTH) + 1; // Months are zero-based in Java's calendar API. int day = cal.get(Calendar.DAY_OF_MONTH); zodiacSign = DateUtil.getZodiac(month, day); } catch (ParseException e) { System.out.println("Error parsing date."); } System.out.printf("The person with this ID was born under the sign of %s.\n", zodiacSign); ``` 这段程序首先利用正则表达式验证输入的身份证明格式是否有效;接着从中抽取生日部分转换成标准日期形式;最后调用Hutool提供的API得出该用户的星座
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值