开发者社区> 问答> 正文

怎样用Calendar返回当前年份

怎样用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());
 }
}
}

展开
收起
蛮大人123 2016-06-13 17:29:57 2250 0
2 条回答
写回答
取消 提交回答
  • public static void main(String[] args) {

            Calendar calendar = Calendar.getInstance();
            System.out.println(calendar.get(Calendar.YEAR));
    }
    2019-07-17 19:36:34
    赞同 展开评论 打赏
  • 我说我不帅他们就打我,还说我虚伪
    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
    }
    }
    2019-07-17 19:36:34
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载