---
title: "根据生日判断星座"
author: "Iam42"
date: "2013-10-15T15:43:02Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-10-15T15:43:02Z"
id: 1958448
url: "https://iam42.iteye.com/blog/1958448"
---

输入String（“yyyy-mm-dd”）

输出String（星座）

```
public static String getConstellation(String birthday) {

        String[] constellationArr = { "水瓶座", "双鱼座", "牡羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座",
                "天蝎座", "射手座", "魔羯座"};

        int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };

        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date birthDay = formatDate.parse(birthday);
            int month = birthDay.getMonth();
            int day = birthDay.getDay();
            if (day < constellationEdgeDay[month]) {
                month = month - 1;
            }
            if (month >= 0) {
                return constellationArr[month];
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return constellationArr[11];
    }
```