准备工作
1.无敌外挂(GitHub直达源码)
Nobb 直击灵魂
https://github.com/xuyishanBD/Java_create_calendar.git
2.maven配置(如果没有走上面的捷径)
    <dependencies>
        <dependency>
            <groupId>net.sourceforge.javacsv</groupId>
            <artifactId>javacsv</artifactId>
            <version>2.0</version>
        </dependency>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
    </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
3.目录设计
简单划分一下目录,app包放的是可执行的代码;bean包放的是JavaBean类;conf包里放的是配置信息
 
源码编写
1.“DateJB.class” 日期类
package bean;
import lombok.Builder;
import lombok.Data;
// lombok的语法糖可以便于生成JavaBean以及建造者模式的构造方法
@Builder
@Data
public class DateJB {
    //属性1:阳历日期
    private String sunCal;
    //属性2:生肖
    private String animalSign;
    //属性3:阴历日期
    private String shadowCal;
    //属性4:星座
    private String constellation;
    //重写toString方法
    public String toString(){
        return sunCal + "  " + animalSign + "  " + shadowCal + "   " + constellation ;
    }
    //header-->写入csv时需要一个表头
    public static String[] header = new String[]{
            "sunCal","animalSign","shadowCal","constellation","distance"
    };
    //将一个JavaBean的各个属性组成一个字符串列表
    public String[] toStringList() {
        return new String[]{
                sunCal,animalSign,shadowCal,constellation
        };
    }
    //获取阳历日期的月日(格式mmdd)
    public String getMonth_day(){
        return sunCal.substring(5);
    }
}
2.“Constellation_JB.class” 星座类
package bean;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@Builder
@Data
@AllArgsConstructor
//星座配置类
public class Constellation_JB {
    //属性1:星座名称
    private String name;
    //属性2:开始日期
    private String start;
    //属性3:结束日期
    private String end;
}
3."cal_shadow.java"主程序类
package app;
import bean.Constellation_JB;
import bean.DateJB;
import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.csvreader.CsvWriter;
import sun.nio.cs.ext.GBK;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
public class cal_shadow {
    public static void main(String[] args) throws IOException {
        //创建一个空列表 列表中存放的对象为DateJB
        ArrayList<DateJB> result_list = new ArrayList<>();
        //调用readConf方法,获取星座对象
        ArrayList<Constellation_JB> conf_lists = CalUtil.readConf();
        String now_String;
        String month_String;
        String day_String;
        int now_year = 1901;
        int now_month = 1;
        int now_day = 1;
        while(now_year <= 2040){
            while(now_month <= 12){
                while(now_day <= CalUtil.getMonthLastDay(now_year,now_month)){
                    month_String = now_month+"";
                    day_String = now_day+"";
                    now_String = now_year
                            + "-"
                            + (month_String.length()==1?("0"+now_month):month_String)
                            + "-"
                            + (day_String.length()==1?("0"+now_day):day_String);
                    DateJB build = DateJB.builder().sunCal(now_String).build();
                    result_list.add(CalUtil.handleDate(build,conf_lists));
                    now_day++;
                }
                now_month++;
                now_day=1;
            }
            now_year++;
            now_month=1;
        }
        CalUtil.createResultCsv(result_list);
    }
}
class CalUtil{
    static DateJB handleDate(DateJB dateJB,ArrayList<Constellation_JB> conf_lists){
        DateTime dateTime = DateUtil.parseDate(dateJB.getSunCal());
        ChineseDate chineseDate = new ChineseDate(dateTime);
        String s = chineseDate.toString();
        dateJB.setShadowCal(s.substring(5));
        dateJB.setAnimalSign(s.substring(2,3));
        dateJB.setConstellation(CalUtil.makeResult(dateJB,conf_lists));
        return dateJB;
    }
    //
    static void createResultCsv(ArrayList<DateJB> check_list) throws IOException {
        CsvWriter cw = new CsvWriter("C:\\Users\\70493\\Desktop\\animalSign.csv", ',', new GBK());
        cw.writeRecord(DateJB.header, true);
        for (DateJB element : check_list) {
            String[] list = element.toStringList();
            cw.writeRecord(list);
        }
        cw.close();
    }
    //
    static int[] run_dayList = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    static int[] dayList = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    static boolean is_run(int year){
        return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
    }
    static int getMonthLastDay(int year,int month){
        if (is_run(year))
            return run_dayList[month-1];
        else
            return dayList[month-1];
    }
    static ArrayList<Constellation_JB> readConf(){
        ArrayList<Constellation_JB> conf_lists = new ArrayList<>();
        try (BufferedReader br = Files.newBufferedReader(Paths.get("conf/constellationConf.csv"), new GBK()))
        {
            //缓冲流单行
            String line;
            while ((line = br.readLine()) != null) {
                //切割
                String[] splits = line.split(",");
                //收纳进集合
                conf_lists.add(new Constellation_JB(splits[0],splits[1],splits[2]));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return conf_lists;
    }
    static String makeResult(DateJB bean, ArrayList<Constellation_JB> conf_lists){
        String month_day = bean.getMonth_day();
        String[] split = month_day.split("-");
        int compare_day = Integer.parseInt(split[0]+split[1]);
        for (Constellation_JB conf_bean : conf_lists) {
            if (compare_day >= Integer.parseInt(conf_bean.getStart())
                    && compare_day <= Integer.parseInt(conf_bean.getEnd())){
                return conf_bean.getName();
            }
        }
        return "摩羯座";
    }
}
4."constellationConf.csv"配置类
白羊座,0321,0419
金牛座,0420,0520
双子座,0521,0621
巨蟹座,0622,0722
狮子座,0723,0822
处女座,0823,0922
天秤座,0923,1023
天蝎座,1024,1122
射手座,1123,1221
魔羯座,1222,0119
水瓶座,0120,0218
双鱼座,0219,0320
5.完成以上后的目录结构

6.注意!一定要注意修改以上代码中的路径
cal_shadow.java 64行 86行



















