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

相关推荐
SuniaWang34 分钟前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
sheji341641 分钟前
【开题答辩全过程】以 基于springboot的扶贫系统为例,包含答辩的问题和答案
java·spring boot·后端
hnlgzb1 小时前
常见的Android Jetpack库会有哪些?这些库中又有哪些常用类的?
android·android jetpack
m0_726965981 小时前
面面面,面面(1)
java·开发语言
xuhaoyu_cpp_java2 小时前
过滤器与监听器学习
java·经验分享·笔记·学习
程序员小假2 小时前
我们来说一下 b+ 树与 b 树的区别
java·后端
Meepo_haha3 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端
sheji34163 小时前
【开题答辩全过程】以 基于springboot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
木井巳3 小时前
【递归算法】子集
java·算法·leetcode·决策树·深度优先