使用DateFormat写的星座查询Java小程序

😂 这篇文章最后更新于2114天前,您需要注意相关的内容是否还可用。

关键词:DateFormat、getTime()

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public class demo {
    public static void main(String[] args) throws ParseException {
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入查询日期(例“2-3”即2月3日):");
            String input = "2000-" + sc.next();     //输入查询,之所以实2000因为其是闰年,2月有29号
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            long birth = sdf.parse(input).getTime();    //解析为Date并获取时间戳
            if (birth >= getTimer("2000-03-21") && birth <= getTimer("2000-04-19")) {
                System.out.println("白羊座");
            } else if (birth >= getTimer("2000-04-20") && birth <= getTimer("2000-05-20")) {
                System.out.println("金牛座");
            } else if (birth >= getTimer("2000-05-21") && birth <= getTimer("2000-06-20")) {
                System.out.println("双子座");
            } else if (birth >= getTimer("2000-06-21") && birth <= getTimer("2000-07-21")) {
                System.out.println("巨蟹座");
            } else if (birth >= getTimer("2000-07-22") && birth <= getTimer("2000-08-22")) {
                System.out.println("狮子座");
            } else if (birth >= getTimer("2000-08-23") && birth <= getTimer("2000-09-22")) {
                System.out.println("处女座");
            } else if (birth >= getTimer("2000-09-23") && birth <= getTimer("2000-10-22")) {
                System.out.println("天秤座");
            } else if (birth >= getTimer("2000-10-23") && birth <= getTimer("2000-11-21")) {
                System.out.println("天蝎座");
            } else if (birth >= getTimer("2000-11-22") && birth <= getTimer("2000-12-21")) {
                System.out.println("射手座");
            } else if (birth >= getTimer("2000-01-20") && birth <= getTimer("2000-02-18")) {
                System.out.println("水瓶座");
            } else if (birth >= getTimer("2000-02-19") && birth <= getTimer("2000-03-20")) {
                System.out.println("双鱼座");
            } else {
                System.out.println("摩羯座");
            }
        }
        public static long getTimer (String date) throws ParseException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.parse(date).getTime();
        }
}