输入年份,自动根据当前日期,返回当前学生是大几,是否毕业

东软学习小组成员:夜枫
入参:年份
返回:字符串
如:2019
返回:你现在是大一下学期
如:2018
返回:你现在是大二下学期

static String a(int year) {
        String res = null;
        int time_year;
        int time_month;
        Date time = new Date();  //2016 2017 2018 2019 2020
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
        res = simpleDateFormat.format(time);
        time_year=Integer.parseInt(res.substring(0,4));
      //  time_year=2019;
       // time_month=10;
        time_month=Integer.parseInt(res.substring(5,7));

        //处理时间超时
       if(year>time_year){
           res="时间太后了,你还没到那个时候入学";

       }
       if(year==time_year){//相同年,但是月份还不到9月
           if(time_month<9){
               res="你还不到入学时间,等九月份你就是大一啦";
           }
           else if(time_month>=9){
               res="你现在是大一上学期";
           }

       }
//2020 3   2019 6
       if(time_year>year){
           int temp=time_year-year;//时差
           if(temp<=3){
               String []Class={"大一","大二","大三","大四"};
               res="你现在是"+Class[temp-1];
               if(time_month<9){
                   res+="下学期";
               }else {
                   if(temp<3){//不是大四
                       res=Class[temp]+"上学期";
                   }else if(temp==3){//大四
                       res="上半年是大四下学期啦,现在已经你毕业几个月了";

                   }

               }
           }
           else if(temp>3){//2020 2019 2018 2017 2016

               int year1=temp-3;//毕业多少年
               int momth1=time_month;//毕业多少月
               if(time_month<6){
                   year1-=1;
                   momth1=time_month+12-6;

               }
               res="你已经毕业"+String.valueOf(year1)+"年"+String.valueOf(momth1)+"月了";
           }

       }

        return res;
    }

java源代码:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
      //  int year = sc.nextInt();
        for(int i=2010;i<2022;i++){
            String t=a(i);
            System.out.println(i+"的结果:-->"+t);
        }

        //System.out.println(t);
    }

   static String a(int year) {
        String res = null;
        int time_year;
        int time_month;
        Date time = new Date();  //2016 2017 2018 2019 2020
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
        res = simpleDateFormat.format(time);
        time_year=Integer.parseInt(res.substring(0,4));
      //  time_year=2019;
       // time_month=10;
        time_month=Integer.parseInt(res.substring(5,7));

        //处理时间超时
       if(year>time_year){
           res="时间太后了,你还没到那个时候入学";

       }
       if(year==time_year){//相同年,但是月份还不到9月
           if(time_month<9){
               res="你还不到入学时间,等九月份你就是大一啦";
           }
           else if(time_month>=9){
               res="你现在是大一上学期";
           }

       }
//2020 3   2019 6
       if(time_year>year){
           int temp=time_year-year;//时差
           if(temp<=3){
               String []Class={"大一","大二","大三","大四"};
               res="你现在是"+Class[temp-1];
               if(time_month<9){
                   res+="下学期";
               }else {
                   if(temp<3){//不是大四
                       res=Class[temp]+"上学期";
                   }else if(temp==3){//大四
                       res="上半年是大四下学期啦,现在已经你毕业几个月了";

                   }

               }
           }
           else if(temp>3){//2020 2019 2018 2017 2016

               int year1=temp-3;//毕业多少年
               int momth1=time_month;//毕业多少月
               if(time_month<6){
                   year1-=1;
                   momth1=time_month+12-6;

               }
               res="你已经毕业"+String.valueOf(year1)+"年"+String.valueOf(momth1)+"月了";
           }

       }

        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值