通过工具类
法①:
package com.xxx.xx;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil{
private final static int[] dayArr = new int[] { 20, 19, 21, 20, 21, 22, 23,
23, 23, 24, 23, 22 };
private final static String[] constellationArr = new String[] { "摩羯座",
"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座",
"天蝎座", "射手座", "摩羯座" };
//根据出生日期计算属相和星座
public static void main(String[] args) {
int month = 10;
int day = 12;
System.out.println("星座为:" + getConstellation(month, day));
System.out.println("属相为:" + getYear(1995));
}
//Java通过生日计算星座
public static String getConstellation(int month, int day) {
return day < dayArr[month - 1] ? constellationArr[month - 1]
: constellationArr[month];
}
//根据生日计算星座
public static String getConstellation(Date bir){
int month = Integer.valueOf(new SimpleDateFormat("MM").format(bir));
int day =Integer.valueOf(new SimpleDateFormat("dd").format(bir));
return getConstellation(month, day);
}
//通过生日计算属相
public static String getYear(int year) {
if (year < 1900) {
return "未知";
}
int start = 1900;
String[] years = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊",
"猴", "鸡", "狗", "猪" };
return years[(year - start) % years.length];
}
public static String getYear(Date date){
int year =Integer.valueOf(new SimpleDateFormat("yyyy").format(date));
return getYear(year);
}
//获取年龄
public static int getAge(Date date){
int year =Integer.valueOf(new SimpleDateFormat("yyyy").format(date));
int now = Integer.valueOf(new SimpleDateFormat("yyyy").format(new Date()));
return now-year;
}
}
法②:
package com.xxx.xx;
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;
}
}