首页 > 代码库 > 常用类的运用(时间类)
常用类的运用(时间类)
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Birthday {
public static void main(String[] args){
System.out.println("你的生日是:1992-10-10");
GregorianCalendar cal=new GregorianCalendar(1992, 10, 10);
System.out.println("你的出生年份:"+cal.get(Calendar.YEAR));
System.out.println("你的出生月份:"+cal.get(Calendar.MONTH));
System.out.println("你的出生日期:"+cal.get(Calendar.DATE));
System.out.println("1900年为鼠年,输入的年份必须大于等于1900");
int nums=(cal.get(Calendar.YEAR)-1900)%12;
switch (nums){
case 0:
System.out.println("你的生肖:鼠");
break;
case 1:
System.out.println("你的生肖:牛");
break;
case 2:
System.out.println("你的生肖:虎");
break;
case 3:
System.out.println("你的生肖:兔");
break;
case 4:
System.out.println("你的生肖:龙");
break;
case 5:
System.out.println("你的生肖:蛇");
break;
case 6:
System.out.println("你的生肖:马");
break;
case 7:
System.out.println("你的生肖:羊");
break;
case 8:
System.out.println("你的生肖:猴");
break;
case 9:
System.out.println("你的生肖:鸡");
break;
case 10:
System.out.println("你的生肖:狗");
break;
case 11:
System.out.println("你的生肖:猪");
break;
}
// 存在逻辑错误
double number=Double.parseDouble(cal.get(Calendar.MONTH)+"."+cal.get(Calendar.DATE));
if(number>=3.21&&number<=4.19)
System.out.println("你是白羊座");
else if(number>=4.20&&number<=5.20)
System.out.println("你是金牛座");
else if(number>=5.21&&number<=6.21)
System.out.println("你是双子座");
else if(number>=6.22&&number<=7.22)
System.out.println("你是巨蟹座");
else if(number>=7.23&&number<=8.22)
System.out.println("你是狮子座");
else if(number>=8.23&&number<=9.22)
System.out.println("你是处女座");
else if(number>=9.23&&number<=10.23)
System.out.println("你是天秤座");
else if(number>=10.24&&number<=11.22)
System.out.println("你是天蝎座");
else if(number>=11.23&&number<=12.21)
System.out.println("你是射手座");
else if(number>=12.22&&number<=12.31||number>=1.01&&number<=1.19)
System.out.println("你是摩羯座");
else if(number>=1.20&&number<=2.18)
System.out.println("你是水瓶座");
else if(number>=2.19&&number<=3.20)
System.out.println("你是双鱼座");
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
Date birthdayDate = null;
try {
birthdayDate = sdf3.parse("1992-10-10");
} catch (ParseException e) {
e.printStackTrace();
}
//得到当前时间:
Calendar now = Calendar.getInstance();
//设置生日时间
Calendar birthdayCal = Calendar.getInstance();
birthdayCal.setTime(birthdayDate);
long diff = Math.abs(now.getTimeInMillis() / (1000 * 3600 * 24) -
birthdayCal.getTimeInMillis() / (1000 * 3600 * 24));
if(now.after(birthdayCal)){
System.out.println("你的生日已经过了" + diff + "天");
}else{
System.out.println("距离你的生日还有" + diff + "天");
}
}
}
常用类的运用(时间类)