怎样用Calendar返回当前年份,程序如下
import java.sql.Date;
import java.util.Calendar;
import java.util.Scanner;
public class InsuranceCheck {
public static void BornYear(int bornyear)throws AgeException{
//why this way cannot use source with the file?
int years = 2016 - bornyear;
if(years-16<0){
throw new AgeException("年龄太小,不用保险!");
}
if(years-16<4){
throw new AgeException("需要缴纳2000元保险费!");
}
else{
throw new AgeException("需要支付1000元保险费!");
}
}
public static void main(String[] args) throws AgeException {
Scanner sc=new Scanner(System.in);
// System.out.println("请输入出生年份:");
// String bornyear=sc.next();
try{
Calendar calendar = Calendar.getInstance();
Date dates = (Date) calendar.getTime();
BornYear(1996);
} catch(AgeException e){
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.YEAR));
}
public class InsuranceCheck {
public static void main(String[] args) throws Exception {
Calendar calendar = Calendar.getInstance();
Date dates = (Date) calendar.getTime();//Thu May 05 20:49:55 CST 2016
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");//yyyy-MM-dd
String year = sdf.format(dates);
System.out.println(year);//2016
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。