s123456m1 2016-05-05 12:18 采纳率: 100%
浏览 1379
已采纳

怎样用Calendar返回当前年份,程序如下

package yichang;

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());

 }
}

}

  • 写回答

2条回答 默认 最新

  • Testdddddddddddddddd 2016-05-05 12:52
    关注

    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
    }
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择