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

相关推荐
鹿角片ljp3 小时前
LeetCode 64:最小路径和复盘|二维 DP 与 ACM 模式完整写法
java·数据结构·算法
泡海椒4 小时前
PDF 表格样式优化:jquick-pdf 边框、圆角、背景色
java·开发语言·pdf
景熙55235 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
wno7046 小时前
Spring Security权限控制
java·python·spring
杨运交6 小时前
[071][验证码模块]基于Spring拦截器的验证码认证设计思想
java·后端·spring
Geek-Chow7 小时前
CountDownLatch in Java
java
SL_staff7 小时前
从RBAC到场景化授权:《无忧·企业文档》三级权限模型的技术实践解析
java·开源·产品
传奇开心果编程8 小时前
【springboot基础语法学与练】第 1 课:从零开始
java·spring boot·后端·学习
SL_staff8 小时前
财务系统慎用低代码?从数据模型闭环看合规落地的技术实践
java·低代码·全栈