蓝桥杯JAVA刷题--001

文章目录

题目需求


2.代码

java 复制代码
class Solution {
    public String convertDateToBinary(String date) {
        if (date == null || date.length() != 10 || date.charAt(4) != '-' || date.charAt(7) != '-') {
            throw new IllegalArgumentException("输入的日期格式不正确,应该是 yyyy - mm - dd");
        }

        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = Integer.parseInt(date.substring(8));

        if (year < 1900 || year > 2100 || month < 1 || month > 12 || day < 1) {
            throw new IllegalArgumentException("输入的日期不在有效范围内(1900 年 1 月 1 日到 2100 年 12 月 31 日)");
        }

        int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            daysInMonth[1] = 29;
        }
        if (day > daysInMonth[month - 1]) {
            throw new IllegalArgumentException("输入的日期不在有效范围内(1900 年 1 月 1 日到 2100 年 12 月 31 日)");
        }

        // 将年、月、日转换为二进制字符串
        String yearBinary = Integer.toBinaryString(year);
        String monthBinary = Integer.toBinaryString(month);
        String dayBinary = Integer.toBinaryString(day);

        // 连接二进制字符串
        return yearBinary + '-' + monthBinary + '-' + dayBinary;
    }
}

3.总结

JAK1.8到时候考场上会给,主要是要熟练使用哇

相关推荐
小邓   ༽18 分钟前
全场景Android测试:API、工具与案例,从TestCase到Mock类应用指南
android·android 测试·android 组件测试·mock 类·测试 api 应用·组件测试核心
摆烂z32 分钟前
Docker与Jib(maven插件版)实战
java
RainbowSea36 分钟前
从 Spring Boot 2.x 到 3.5.x + JDK21:一次完整的生产环境迁移实战
java·spring boot·后端
笨手笨脚の39 分钟前
Spring Core常见错误及解决方案
java·后端·spring
奶油松果42 分钟前
Springboot自动装配 - redis和redission
java·spring boot·redis
霍夫曼1 小时前
UTC时间与本地时间转换问题
java·linux·服务器·前端·javascript
VX:Fegn08951 小时前
计算机毕业设计|基于Java人力资源管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端·课程设计
荔枝hu1 小时前
springboot和shiro组合引入SseEmitter的一些坑
java·spring boot·后端·sseeitter
老华带你飞2 小时前
健身房|基于springboot + vue健身房管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端