Java根据出生日期计算星座:
/**
* Java通过生日计算星座
*
* @param birthday 2021-09-02
* @return
*/
public static String getConstellation(Date birthday) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(birthday);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int[] dayArr = new int[] { 20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22 };
String[] constellationArr = new String[] { "摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座",
"天蝎座", "射手座", "摩羯座" };
return day < dayArr[month - 1] ? constellationArr[month - 1] : constellationArr[month];
}
|