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

相关推荐
0xDevNull13 小时前
Tomcat 运行原理与架构深度解析
java·架构·tomcat
svdo1250p13 小时前
“Fatal error: require(): Failed opening required...” 以及如何彻底避免它再次出现
android·ide·android studio
ch.ju13 小时前
Java程序设计(第3版)第三章——数组
java·开发语言
_waylau13 小时前
“Java+AI全栈工程师”问答01:Spring MVC登录页面错误提示
java·开发语言·vue.js·后端·spring·mvc·springcloud
Giggle121813 小时前
上门家政服务平台 | 多端协同,源码交付,用户端小程序+H5、服务端APP、管理后台
java·小程序·架构·产品运营·个人开发
李斯维13 小时前
工厂设计模式(Factory Pattern):工厂方法与抽象工厂的实例演示
java·设计模式
myloveasuka13 小时前
通配符 “?“
java
AI人工智能+电脑小能手13 小时前
【大白话说Java面试题 第41题】【JVM篇】第1题:JVM由哪些部分组成?
java·开发语言·jvm·后端·面试
Digitally13 小时前
如何将 POCO 手机同步到电脑?
android
0xDevNull13 小时前
ConcurrentHashMap 与 Hashtable 深度对比
java·开发语言