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

跟上篇的思路差不多,只是换了个表示时间的方式,这次用gettime获取时间戳,在某段时间戳内的日期为某星座。

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

public class demo {    // blog: cway.top
    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();
        }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值