lihang
2023-04-25 3fade6d3b27f5666672bb3af610020367f790bda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.vci.ubcs.starter.web.util;
 
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
 
//package com.vci.starter.web.util;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class Lunar {
    private int year;
    private int month;
    private int day;
    private boolean leap;
    static final String[] chineseNumber = new String[]{"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
    static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
    static final long[] lunarInfo = new long[]{19416L, 19168L, 42352L, 21717L, 53856L, 55632L, 91476L, 22176L, 39632L, 21970L, 19168L, 42422L, 42192L, 53840L, 119381L, 46400L, 54944L, 44450L, 38320L, 84343L, 18800L, 42160L, 46261L, 27216L, 27968L, 109396L, 11104L, 38256L, 21234L, 18800L, 25958L, 54432L, 59984L, 28309L, 23248L, 11104L, 100067L, 37600L, 116951L, 51536L, 54432L, 120998L, 46416L, 22176L, 107956L, 9680L, 37584L, 53938L, 43344L, 46423L, 27808L, 46416L, 86869L, 19872L, 42448L, 83315L, 21200L, 43432L, 59728L, 27296L, 44710L, 43856L, 19296L, 43748L, 42352L, 21088L, 62051L, 55632L, 23383L, 22176L, 38608L, 19925L, 19152L, 42192L, 54484L, 53840L, 54616L, 46400L, 46496L, 103846L, 38320L, 18864L, 43380L, 42160L, 45690L, 27216L, 27968L, 44870L, 43872L, 38256L, 19189L, 18800L, 25776L, 29859L, 59984L, 27480L, 21952L, 43872L, 38613L, 37600L, 51552L, 55636L, 54432L, 55888L, 30034L, 22176L, 43959L, 9680L, 37584L, 51893L, 43344L, 46240L, 47780L, 44368L, 21977L, 19360L, 42416L, 86390L, 21168L, 43312L, 31060L, 27296L, 44368L, 23378L, 19296L, 42726L, 42208L, 53856L, 60005L, 54576L, 23200L, 30371L, 38608L, 19415L, 19152L, 42192L, 118966L, 53840L, 54560L, 56645L, 46496L, 22224L, 21938L, 18864L, 42359L, 42160L, 43600L, 111189L, 27936L, 44448L};
 
    private static final int yearDays(int y) {
        int sum = 348;
 
        for(int i = 32768; i > 8; i >>= 1) {
            if ((lunarInfo[y - 1900] & (long)i) != 0L) {
                ++sum;
            }
        }
 
        return sum + leapDays(y);
    }
 
    private static final int leapDays(int y) {
        if (leapMonth(y) != 0) {
            return (lunarInfo[y - 1900] & 65536L) != 0L ? 30 : 29;
        } else {
            return 0;
        }
    }
 
    private static final int leapMonth(int y) {
        return (int)(lunarInfo[y - 1900] & 15L);
    }
 
    private static final int monthDays(int y, int m) {
        return (lunarInfo[y - 1900] & (long)(65536 >> m)) == 0L ? 29 : 30;
    }
 
    public final String animalsYear() {
        String[] Animals = new String[]{"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
        return Animals[(this.year - 4) % 12];
    }
 
    private static final String cyclicalm(int num) {
        String[] Gan = new String[]{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
        String[] Zhi = new String[]{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
        return Gan[num % 10] + Zhi[num % 12];
    }
 
    public final String cyclical() {
        int num = this.year - 1900 + 36;
        return cyclicalm(num);
    }
 
    public Lunar(Calendar cal) {
        //int leapMonth = false;
        Date baseDate = null;
 
        try {
            baseDate = chineseDateFormat.parse("1900年1月31日");
        } catch (ParseException var12) {
            var12.printStackTrace();
        }
 
        int offset = (int)((cal.getTime().getTime() - baseDate.getTime()) / 86400000L);
        int dayCyl = offset + 40;
        int monCyl = 14;
        int daysOfYear = 0;
 
        int iYear;
        for(iYear = 1900; iYear < 2050 && offset > 0; ++iYear) {
            daysOfYear = yearDays(iYear);
            offset -= daysOfYear;
            monCyl += 12;
        }
 
        if (offset < 0) {
            offset += daysOfYear;
            --iYear;
            monCyl -= 12;
        }
 
        this.year = iYear;
        int yearCyl = iYear - 1864;
        int leapMonth = leapMonth(iYear);
        this.leap = false;
        int daysOfMonth = 0;
 
        int iMonth;
        for(iMonth = 1; iMonth < 13 && offset > 0; ++iMonth) {
            if (leapMonth > 0 && iMonth == leapMonth + 1 && !this.leap) {
                --iMonth;
                this.leap = true;
                daysOfMonth = leapDays(this.year);
            } else {
                daysOfMonth = monthDays(this.year, iMonth);
            }
 
            offset -= daysOfMonth;
            if (this.leap && iMonth == leapMonth + 1) {
                this.leap = false;
            }
 
            if (!this.leap) {
                ++monCyl;
            }
        }
 
        if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
            if (this.leap) {
                this.leap = false;
            } else {
                this.leap = true;
                --iMonth;
                --monCyl;
            }
        }
 
        if (offset < 0) {
            offset += daysOfMonth;
            --iMonth;
            --monCyl;
        }
 
        this.month = iMonth;
        this.day = offset + 1;
    }
 
    public static String getChinaDayString(int day) {
        String[] chineseTen = new String[]{"初", "十", "廿", "卅"};
        int n = day % 10 == 0 ? 9 : day % 10 - 1;
        if (day > 30) {
            return "";
        } else {
            return day == 10 ? "初十" : chineseTen[day / 10] + chineseNumber[n];
        }
    }
 
    @Override
    public String toString() {
        String m = chineseNumber[this.month - 1];
        if (m != null && m.trim().equals("一")) {
            m = "正";
        }
 
        if (m != null && m.trim().equals("十一")) {
            m = "冬";
        }
 
        if (m != null && m.trim().equals("十二")) {
            m = "腊";
        }
 
        return this.year + this.cyclical() + this.animalsYear() + "年" + (this.leap ? "闰" : "") + m + "月" + getChinaDayString(this.day);
    }
 
    public String getDate() {
        String m = chineseNumber[this.month - 1];
        if (m != null && m.trim().equals("一")) {
            m = "正";
        }
 
        if (m != null && m.trim().equals("十一")) {
            m = "冬";
        }
 
        if (m != null && m.trim().equals("十二")) {
            m = "腊";
        }
 
        return this.animalsYear() + "年" + (this.leap ? "闰" : "") + m + "月" + getChinaDayString(this.day);
    }
 
    public static void main(String[] args) throws ParseException {
        Calendar today = Calendar.getInstance();
        today.setTime(chineseDateFormat.parse("2013年2月1日"));
        Lunar lunar = new Lunar(today);
        System.out.println(lunar.toString());
        System.out.println("北京时间:" + chineseDateFormat.format(today.getTime()) + " 农历" + lunar);
    }
}