【练习】用户按照格式输入出生年、月、日、时、分、秒,计算他几岁了

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

public class Test04 {
    public static void main(String[] args) throws ParseException {
        // 获取当前年份
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        Scanner input = new Scanner(System.in);
        // 获取用户输入的出生年、月、日、时、分、秒
        System.out.println("请依次输入您的出生年、月、日、时、分、秒");
        System.out.print("年:");
        int birthYear = input.nextInt();
        System.out.print("月:");
        int birthMonth = input.nextInt();
        System.out.print("日:");
        int birthDay = input.nextInt();
        System.out.print("时:");
        int birthHours = input.nextInt();
        System.out.print("分:");
        int birthMinutes = input.nextInt();
        System.out.print("秒:");
        int birthSeconds = input.nextInt();

        String check = check(birthYear, birthMonth, birthDay, birthHours, birthMinutes, birthSeconds);
        // 年龄等于当前年份减去出生年份
        int age = year - birthYear;

        // 格式化当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        Date date = new Date();
        String nowTime = sdf.format(date);
        // 将日期使用'-'分隔 传入String数组
        String[] nowTimeArr = nowTime.split("-");
        // 声明一个int数组 长度和String数组一样
        int[] nowTimeArr2 = new int[nowTimeArr.length];
        String[] birthDateArr = check.split("-");
        int[] birthDateArr2 = new int[birthDateArr.length];
        // 将String中的每个元素都遍历到int数组中
        for (int i = 0; i < nowTimeArr.length; i++) {
            nowTimeArr2[i] = Integer.parseInt(nowTimeArr[i]);
            birthDateArr2[i] = Integer.parseInt(birthDateArr[i]);
        }
        // 从int数组中下标为1的数字开始依次做比较
        for (int i = 1; i < nowTimeArr2.length; i++) {
            // 如果下标相同且当前时间大于出生时间则结束循环 如果不大于进行else if判断
            if (nowTimeArr2[i] > birthDateArr2[i]) {
                break;
            } else if (nowTimeArr2[i] == birthDateArr2[i]) {
                // 如果相同则结束本次循环 下一个日期进行比较
                continue;
            } else {
                // 如果循环一边都不大于则跳出循环 并且年龄-1
                age -= 1;
                break;
            }
        }
        System.out.println("你今年"+age+"岁了");
    }

    /*
     * 定义一个方法 
     * 检查日期格式是否符合规范
     * */
    public static String check(int year, int month, int day, int hours, int minutes, int seconds) throws ParseException {
        Calendar cal = Calendar.getInstance();
        // 获取系统当前时间
        int y = cal.get(Calendar.YEAR);
        // 获取某年某月有多少天
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        cal.setTime(sdf.parse(year + "-" + month));
        int d = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 如果传来的参数year大于当前年份 则证明格式错误
        if (year > y) {
            return "年份格式错误";
        } else if (month < 1 || month > 12) {
            return "月份格式错误";
        } else if (day < 1 || day > d) {
            return "日期格式错误";
        } else if (hours < 0 || hours > 23) {
            return "小时格式错误";
        } else if (minutes < 0 || minutes > 59) {
            return "分钟格式错误";
        } else if (seconds < 0 || seconds > 59) {
            return "秒格式错误";
        }
        String birth = year + "-" + month + "-" + day + "-" + hours + "-" + minutes + "-" + seconds;
        return birth;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值