MySQL获取月份,年份中的日期列表

MySQL获取月份,年份中的日期列表,第1张

MySQL获取月份,年份中的日期列表

这是此问题的mysql / java解决方案。

创建表语句:

CREATE TABLE `date_table` (  `ID` bigint(20) NOT NULL AUTO_INCREMENT,  `date` date NOT NULL,  PRIMARY KEY (`ID`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

Java代码:

public static void main(String[] args) throws ClassNotFoundException, SQLException {        Database db = new Database("root", "root01", "test-db", "10.10.5.11");        db.connect();        int start_year = 2013;        int end_year = 2100;        for (int year = start_year; year < end_year; year++) { for (int month = 1; month <= 12; month++) {     int days_in_month = 0;     switch (month) {         case 1: { //January  days_in_month = 31;  break;         }         case 2: { //February  if (year % 4 == 0) {      days_in_month = 29;  } else {      days_in_month = 28;  }  break;         }         case 3: { //March  days_in_month = 31;  break;         }         case 4: { //April  days_in_month = 30;  break;         }         case 5: { //May  days_in_month = 31;  break;         }         case 6: { //June  days_in_month = 30;  break;         }         case 7: { //July  days_in_month = 31;  break;         }         case 8: { //August  days_in_month = 31;  break;         }         case 9: { //September  days_in_month = 30;  break;         }         case 10: { //October  days_in_month = 31;  break;         }         case 11: { //November  days_in_month = 30;  break;         }         case 12: { //December  days_in_month = 31;  break;         }     }     for (int day = 1; day <= days_in_month; day++) {         db.executeInsert("INSERT INTO date_table (date) VALUES ('" + year + "-" + month + "-" + day + "');");     } } System.out.println("YEAR: " + year + " FINISHED");        }    }

我使用此sql获取日期列表:

SELECt * FROM date_table WHERe YEAR(date) = 2014 AND MonTH(date) = 2;

希望有人使用它:)



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5431809.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存