码迷,mamicode.com
首页 > 编程语言 > 详细

给定公历日期计算12星座(Java代码实现)

时间:2015-02-23 09:43:30      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:java

给定公历日期,计算返回该日期对应的十二星座。

public static String getXingZuoName(int month, int day);

注意:参数month和day均为公历月、日。

返回的中文星座名如:处女座。


import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;

//import	android.util.Log;

public class XingZuo {
	private static List<Map<String, Object>> LIST = null;
	private static float[][] floatXingZuo = null;

	public XingZuo() {
		// LIST=loadXingZuo12();
		// floatXingZuo=loadXingZuoFloatValue();
	}

	public static String getXingZuoName(int month, int day) {
		if (floatXingZuo == null)
			floatXingZuo = loadXingZuoFloatValue();

		String ds = String.format("%02d", day);
		float target = Float.valueOf(month + "." + ds);

		boolean bool1 = Float.compare(target, 12.22f) >= 0 ? true : false;
		boolean bool2 = Float.compare(target, 1.20f) <= 0 ? true : false;
		if (bool1 || bool2) {
			return "摩羯座";
		}

		int i;
		boolean b1, b2;
		for (i = 0; i < 12; i++) {

			b1 = Float.compare(target, floatXingZuo[0][i]) >= 0 ? true : false;
			b2 = Float.compare(target, floatXingZuo[1][i]) <= 0 ? true : false;

			if (b1 && b2)
				break;
		}

		Map map = (Map) (LIST.get(i));

		return map.get("XINGZUO_NAME") + "";
	}

	private static float[][] loadXingZuoFloatValue() {
		if (LIST == null)
			LIST = loadXingZuo12();

		Map map = null;
		// String xzname=null;
		int mind, maxd, minm, maxm;

		float[][] fxingzuo = new float[2][12];

		float start, end;
		for (int i = 0; i < 12; i++) {
			map = (Map) (LIST.get(i));

			mind = Integer.parseInt(map.get("MIN_DAY") + "");
			maxd = Integer.parseInt(map.get("MAX_DAY") + "");
			minm = Integer.parseInt(map.get("MIN_MONTH") + "");
			maxm = Integer.parseInt(map.get("MAX_MONTH") + "");

			start = Float.valueOf(minm + "." + mind);
			end = Float.valueOf(maxm + "." + maxd);

			fxingzuo[0][i] = start;
			fxingzuo[1][i] = end;
		}

		return fxingzuo;
	}

	private static List loadXingZuo12() {
		String XINGZUO_NAME = "XINGZUO_NAME";

		String MIN_DAY = "MIN_DAY";
		String MIN_MONTH = "MIN_MONTH";
		String MAX_DAY = "MAX_DAY";
		String MAX_MONTH = "MAX_MONTH";

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "白羊座");
		map.put(MIN_DAY, 21);
		map.put(MAX_DAY, 20);
		map.put(MIN_MONTH, 3);
		map.put(MAX_MONTH, 4);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "金牛座");
		map.put(MIN_DAY, 21);
		map.put(MAX_DAY, 21);
		map.put(MIN_MONTH, 4);
		map.put(MAX_MONTH, 5);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "双子座");
		map.put(MIN_DAY, 22);
		map.put(MAX_DAY, 21);
		map.put(MIN_MONTH, 5);
		map.put(MAX_MONTH, 6);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "巨蟹座");
		map.put(MIN_DAY, 22);
		map.put(MAX_DAY, 22);
		map.put(MIN_MONTH, 6);
		map.put(MAX_MONTH, 7);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "狮子座");
		map.put(MIN_DAY, 23);
		map.put(MAX_DAY, 23);
		map.put(MIN_MONTH, 7);
		map.put(MAX_MONTH, 8);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "处女座");
		map.put(MIN_DAY, 24);
		map.put(MAX_DAY, 23);
		map.put(MIN_MONTH, 8);
		map.put(MAX_MONTH, 9);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "天秤座");
		map.put(MIN_DAY, 24);
		map.put(MAX_DAY, 23);
		map.put(MIN_MONTH, 9);
		map.put(MAX_MONTH, 10);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "天蝎座");
		map.put(MIN_DAY, 24);
		map.put(MAX_DAY, 22);
		map.put(MIN_MONTH, 10);
		map.put(MAX_MONTH, 11);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "射手座");
		map.put(MIN_DAY, 23);
		map.put(MAX_DAY, 21);
		map.put(MIN_MONTH, 11);
		map.put(MAX_MONTH, 12);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "摩羯座");
		map.put(MIN_DAY, 22);
		map.put(MAX_DAY, 20);
		map.put(MIN_MONTH, 12);
		map.put(MAX_MONTH, 1);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "水瓶座");
		map.put(MIN_DAY, 21);
		map.put(MAX_DAY, 19);
		map.put(MIN_MONTH, 1);
		map.put(MAX_MONTH, 2);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put(XINGZUO_NAME, "双鱼座");
		map.put(MIN_DAY, 20);
		map.put(MAX_DAY, 20);
		map.put(MIN_MONTH, 2);
		map.put(MAX_MONTH, 3);
		list.add(map);

		return list;
	}
}



给定公历日期计算12星座(Java代码实现)

标签:java

原文地址:http://blog.csdn.net/zhangphil/article/details/43908119

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!