蓝桥杯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到时候考场上会给,主要是要熟练使用哇

相关推荐
吃饱了得干活6 小时前
Spring Cloud Gateway 微服务网关:路由、断言、过滤器
java·spring cloud
李斯维7 小时前
从历史的角度看 Android 软件架构
android·架构·android jetpack
lwx572808 小时前
探秘InnoDB:搞懂它的内存、线程、磁盘与日志刷盘策略
java·后端
Flynt9 小时前
从Spring Boot 4.0升到4.1,我在Maven和gRPC上栽了跟头
java·spring boot·后端
plainGeekDev10 小时前
Activity 间传值 → Navigation 参数
android·java·kotlin
用户416596736935510 小时前
Android WebView 加载 file:// 离线页面调试教程
android·前端
plainGeekDev10 小时前
onActivityResult → ActivityResult API
android·java·kotlin
Sunia10 小时前
《AgentX 专栏》10-生产部署:3台2C4G云服务器把企业级Agent真正跑起来的完整方案
java·架构
ZhengEnCi11 小时前
J7A-高级Java工程师面试三道灵魂拷问-深度广度与工程素养的终极检验
java·后端
随遇丿而安14 小时前
第10周:Activity 基础功能与生命周期优化
android