跟上篇的思路差不多,只是换了个表示时间的方式,这次用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();
}
}
本文介绍了一个使用Java编写的简单程序,该程序通过输入特定日期来判断对应的星座,并使用了时间戳的方法来进行日期比较。程序首先将用户输入的月份和日期转换成2000年的完整日期格式,然后通过与预设的星座区间进行比较,最终输出相应的星座。

7666

被折叠的 条评论
为什么被折叠?



