https://blog.csdn.net/d18865556765/article/details/52156330?utm_medium=distribute.pc_relevant_download.none-task-blog-baidujs-2.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-baidujs-2.nonecase

    1. package myCalendar;
    2. import java.awt.*;
    3. import java.awt.event.ActionEvent;
    4. import java.awt.event.ActionListener;
    5. import java.text.ParseException;
    6. import java.text.SimpleDateFormat;
    7. import java.util.Calendar;
    8. import java.util.Date;
    9. import javax.swing.JComboBox;
    10. import javax.swing.JFrame;
    11. import javax.swing.JLabel;
    12. import javax.swing.JPanel;
    13. import javax.swing.JButton;
    14. public class Lunar {
    15. private int year;
    16. private int month;
    17. private int day;
    18. private boolean leap;
    19. final static String chineseNumber[] = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
    20. static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
    21. final static long[] lunarInfo = new long[]
    22. {0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
    23. 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
    24. 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
    25. 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,
    26. 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,
    27. 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
    28. 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
    29. 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
    30. 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
    31. 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
    32. 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
    33. 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
    34. 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
    35. 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
    36. 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0};
    37. //====== 传回农历 y年的总天数
    38. final private static int yearDays(int y) {
    39. int i, sum = 348;
    40. for (i = 0x8000; i > 0x8; i >>= 1) {
    41. if ((lunarInfo[y - 1900] & i) != 0) sum += 1;
    42. }
    43. return (sum + leapDays(y));
    44. }
    45. //====== 传回农历 y年闰月的天数
    46. final private static int leapDays(int y) {
    47. if (leapMonth(y) != 0) {
    48. if ((lunarInfo[y - 1900] & 0x10000) != 0)
    49. return 30;
    50. else
    51. return 29;
    52. } else
    53. return 0;
    54. }
    55. //====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
    56. final private static int leapMonth(int y) {
    57. return (int) (lunarInfo[y - 1900] & 0xf);
    58. }
    59. //====== 传回农历 y年m月的总天数
    60. final private static int monthDays(int y, int m) {
    61. if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
    62. return 29;
    63. else
    64. return 30;
    65. }
    66. //====== 传回农历 y年的生肖
    67. final public String animalsYear() {
    68. final String[] Animals = new String[]{"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
    69. return Animals[(year - 4) % 12];
    70. }
    71. //====== 传入 月日的offset 传回干支, 0=甲子
    72. final private static String cyclicalm(int num) {
    73. final String[] Gan = new String[]{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
    74. final String[] Zhi = new String[]{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
    75. return (Gan[num % 10] + Zhi[num % 12]);
    76. }
    77. //====== 传入 offset 传回干支, 0=甲子
    78. final public String cyclical() {
    79. int num = year - 1900 + 36;
    80. return (cyclicalm(num));
    81. }
    82. public static String getChinaDayString(int day) {
    83. String chineseTen[] = {"初", "十", "廿", "卅"};
    84. int n = day % 10 == 0 ? 9 : day % 10 - 1;
    85. if (day > 30)
    86. return "";
    87. if (day == 10)
    88. return "初十";
    89. else
    90. return chineseTen[day / 10] + chineseNumber[n];
    91. }
    92. /** */
    93. /**
    94. * 传出y年m月d日对应的农历.
    95. * yearCyl3:农历年与1864的相差数 ?
    96. * monCyl4:从1900年1月31日以来,闰月数
    97. * dayCyl5:与1900年1月31日相差的天数,再加40 ?
    98. *
    99. * @param
    100. * @return
    101. */
    102. public String getLunarDate(int year_log, int month_log, int day_log) {
    103. //@SuppressWarnings("unused")
    104. int yearCyl, monCyl, dayCyl;
    105. int leapMonth = 0;
    106. String nowadays;
    107. Date baseDate = null;
    108. Date nowaday = null;
    109. try {
    110. baseDate = chineseDateFormat.parse("1900年1月31日");
    111. } catch (ParseException e) {
    112. e.printStackTrace(); //To change body of catch statement use Options | File Templates.
    113. }
    114. nowadays = year_log + "年" + month_log + "月" + day_log + "日";
    115. try {
    116. nowaday = chineseDateFormat.parse(nowadays);
    117. } catch (ParseException e) {
    118. e.printStackTrace(); //To change body of catch statement use Options | File Templates.
    119. }
    120. //求出和1900年1月31日相差的天数
    121. int offset = (int) ((nowaday.getTime() - baseDate.getTime()) / 86400000L);
    122. dayCyl = offset + 40;
    123. monCyl = 14;
    124. //用offset减去每农历年的天数
    125. // 计算当天是农历第几天
    126. //i最终结果是农历的年份
    127. //offset是当年的第几天
    128. int iYear, daysOfYear = 0;
    129. for (iYear = 1900; iYear < 10000 && offset > 0; iYear++) {
    130. daysOfYear = yearDays(iYear);
    131. offset -= daysOfYear;
    132. monCyl += 12;
    133. }
    134. if (offset < 0) {
    135. offset += daysOfYear;
    136. iYear--;
    137. monCyl -= 12;
    138. }
    139. //农历年份
    140. year = iYear;
    141. yearCyl = iYear - 1864;
    142. leapMonth = leapMonth(iYear); //闰哪个月,1-12
    143. leap = false;
    144. //用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天
    145. int iMonth, daysOfMonth = 0;
    146. for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {
    147. //闰月
    148. if (leapMonth > 0 && iMonth == (leapMonth + 1) && ! leap) {
    149. -- iMonth;
    150. leap = true;
    151. daysOfMonth = leapDays(year);
    152. } else
    153. daysOfMonth = monthDays(year, iMonth);
    154. offset -= daysOfMonth;
    155. //解除闰月
    156. if (leap && iMonth == (leapMonth + 1)) leap = false;
    157. if (! leap) monCyl++;
    158. }
    159. //offset为0时,并且刚才计算的月份是闰月,要校正
    160. if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
    161. if (leap) {
    162. leap = false;
    163. } else {
    164. leap = true;
    165. -- iMonth;
    166. -- monCyl;
    167. }
    168. }
    169. //offset小于0时,也要校正
    170. if (offset < 0) {
    171. offset += daysOfMonth;
    172. -- iMonth;
    173. -- monCyl;
    174. }
    175. month = iMonth;
    176. day = offset + 1;
    177. if (((month) == 1) && day == 1) {
    178. return "春节";
    179. } else if (((month) == 1) && day == 15) {
    180. return "元宵";
    181. } else if (((month) == 5) && day == 5)
    182. return "端午";
    183. else if (((month) == 8) && day == 15)
    184. return "中秋";
    185. else if (day == 1)
    186. return chineseNumber[month - 1] + "月";
    187. else
    188. return getChinaDayString(day);
    189. }
    190. public String toString() {
    191. if (chineseNumber[month - 1] == "一" && getChinaDayString(day) == "初一")
    192. return "农历" + year + "年";
    193. else if (getChinaDayString(day) == "初一")
    194. return chineseNumber[month - 1] + "月";
    195. else
    196. return getChinaDayString(day);
    197. //return year + "年" + (leap ? "闰" : "") + chineseNumber[month - 1] + "月" + getChinaDayString(day);
    198. }
    199. }
    200. class Clock extends Canvas implements Runnable {
    201. /**
    202. *
    203. */
    204. private static final long serialVersionUID = 3660124045489727166L;
    205. MainFrame mf;
    206. Thread t;
    207. String time;
    208. public Clock(MainFrame mf) {
    209. this.mf = mf;
    210. setSize(280, 40);
    211. setBackground(Color.white);
    212. t = new Thread(this); //实例化线程
    213. t.start(); //调用线程
    214. }
    215. public void run() {
    216. while (true) {
    217. try {
    218. Thread.sleep(1000); //休眠1秒钟
    219. } catch (InterruptedException e) {
    220. System.out.println("异常");
    221. }
    222. this.repaint(100);
    223. }
    224. }
    225. public void paint(Graphics g) {
    226. Font f = new Font("宋体", Font.BOLD, 16);
    227. SimpleDateFormat SDF = new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型
    228. Calendar now = Calendar.getInstance();
    229. time = SDF.format(now.getTime()); //得到当前日期和时间
    230. g.setFont(f);
    231. g.setColor(Color.black);
    232. g.drawString(time, 45, 25);
    233. }
    234. }
    235. class MainFrame extends JFrame {
    236. /**
    237. *
    238. */
    239. private static final long serialVersionUID = 1L;
    240. JPanel panel = new JPanel(new BorderLayout());
    241. JPanel panel1 = new JPanel();
    242. JPanel panel2 = new JPanel(new GridLayout(7, 7));
    243. JPanel panel3 = new JPanel();
    244. JLabel[] label = new JLabel[49];
    245. JLabel y_label = new JLabel("年份");
    246. JLabel m_label = new JLabel("月份");
    247. JComboBox com1 = new JComboBox();
    248. JComboBox com2 = new JComboBox();
    249. JButton but1 = new JButton("上个月");
    250. JButton but2 = new JButton("下个月");
    251. int re_year, re_month;
    252. int x_size, y_size;
    253. String year_num;
    254. Calendar now = Calendar.getInstance(); // 实例化Calendar
    255. MainFrame() {
    256. super("万年历");
    257. setSize(600, 700);
    258. x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
    259. y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
    260. setLocation((x_size - 300) / 2, (y_size - 350) / 2);
    261. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    262. panel1.add(but1);
    263. panel1.add(y_label);
    264. panel1.add(com1);
    265. panel1.add(m_label);
    266. panel1.add(com2);
    267. panel1.add(but2);
    268. for (int i = 0; i < 49; i++) {
    269. label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中
    270. panel2.add(label[i]);
    271. }
    272. panel3.add(new Clock(this));
    273. panel.add(panel1, BorderLayout.NORTH);
    274. panel.add(panel2, BorderLayout.CENTER);
    275. panel.add(panel3, BorderLayout.SOUTH);
    276. panel.setBackground(Color.white);
    277. panel1.setBackground(Color.white);
    278. panel2.setBackground(Color.white);
    279. panel3.setBackground(Color.gray);
    280. Init();
    281. but1.addActionListener(new AnAction());
    282. but2.addActionListener(new AnAction());
    283. com1.addActionListener(new ClockAction());
    284. com2.addActionListener(new ClockAction());
    285. setContentPane(panel);
    286. setVisible(true);
    287. setResizable(false);
    288. }
    289. class AnAction implements ActionListener {
    290. public void actionPerformed(ActionEvent e) {
    291. int c_year, c_month, c_week;
    292. c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
    293. c_year = Integer.parseInt(com1.getSelectedItem().toString()) - 1; // 得到当前所选年份
    294. if (e.getSource() == but1) {
    295. if (c_month == 0) {
    296. c_year = c_year - 1;
    297. c_month = 11;
    298. } else
    299. c_month = c_month - 1;
    300. }
    301. if (e.getSource() == but2) {
    302. if (c_month == 11) {
    303. c_year = c_year + 1;
    304. c_month = 0;
    305. } else
    306. c_month = c_month + 1;
    307. }
    308. com1.setSelectedIndex(c_year);
    309. com2.setSelectedIndex(c_month);
    310. c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份
    311. c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
    312. c_week = use(c_year, c_month); // 调用函数use,得到星期几
    313. Resetday(c_week, c_year, c_month); // 调用函数Resetday
    314. }
    315. }
    316. /* class NextAction implements ActionListener
    317. {
    318. public void actionPerformed(ActionEvent arg0)
    319. {
    320. int c_year, c_month,c_week;
    321. c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
    322. c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份
    323. if(c_month==11)
    324. {
    325. c_year=c_year+1;
    326. c_month=0;
    327. }
    328. c_week = use(c_year, c_month); // 调用函数use,得到星期几
    329. Resetday(c_week, c_year, c_month); // 调用函数Resetday
    330. }
    331. }
    332. */
    333. class ClockAction implements ActionListener {
    334. public void actionPerformed(ActionEvent arg0) {
    335. int c_year, c_month, c_week;
    336. c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份
    337. c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
    338. c_week = use(c_year, c_month); // 调用函数use,得到星期几
    339. Resetday(c_week, c_year, c_month); // 调用函数Resetday
    340. }
    341. }
    342. public void Init() {
    343. int year, month_num, first_day_num;
    344. String log[] = {"日", "一", "二", "三", "四", "五", "六"};
    345. for (int i = 0; i < 7; i++) {
    346. label[i].setText(log[i]);
    347. }
    348. for (int i = 0; i < 49; i = i + 7) {
    349. label[i].setForeground(Color.red); // 将星期日的日期设置为红色
    350. }
    351. for (int i = 6; i < 49; i = i + 7) {
    352. label[i].setForeground(Color.blue);// 将星期六的日期设置为hong色
    353. }
    354. for (int i = 1; i < 10000; i++) {
    355. com1.addItem("" + i);
    356. }
    357. for (int i = 1; i < 13; i++) {
    358. com2.addItem("" + i);
    359. }
    360. month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份
    361. year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份
    362. com1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年???????????
    363. com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月
    364. first_day_num = use(year, month_num);
    365. Resetday(first_day_num, year, month_num);
    366. }
    367. public int use(int reyear, int remonth) {
    368. int week_num;
    369. now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天
    370. week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期
    371. return week_num;
    372. }
    373. // @SuppressWarnings("deprecation")
    374. public void Resetday(int week_log, int year_log, int month_log) {
    375. String[][] riLI = new String[49][49];
    376. String log[] = {"日", "一", "二", "三", "四", "五", "六"};
    377. for (int i = 0; i < 7; i++) {
    378. riLI[0][i] = (log[i]);
    379. }
    380. int month_day_score; // 存储月份的天数
    381. int count;
    382. Lunar lunar;
    383. int month_day;
    384. String[] LunarDate = new String[49];
    385. month_day_score = 0;
    386. count = 1;
    387. for (int i = 1; i < 49; i++) {
    388. for (int j = 0; j < 49; j = j + 7) {
    389. if (i != j && i != j + 6)
    390. label[i].setForeground(Color.black);
    391. }
    392. }
    393. Date date = new Date(year_log, month_log + 1, 1); // now MONTH是从0开始的, 对于一月第几天来说,DAY_OF_MONTH第一天就是1. 对于一年第几个月来说,MONTH一月份是0,二月份是1...
    394. Calendar cal = Calendar.getInstance();
    395. cal.setTime(date);
    396. cal.add(Calendar.MONTH, - 1); // 前个月
    397. month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天
    398. month_day = month_day_score;
    399. for (int i = 7; i < 49; i++) { // 初始化标签
    400. label[i].setText("");
    401. }
    402. week_log = week_log + 6; // 将星期数加6,使显示正确
    403. month_day_score = month_day_score + week_log;
    404. lunar = new Lunar();
    405. for (int i = 0; i < month_day; i++) {
    406. LunarDate[i] = lunar.getLunarDate(year_log, month_log + 1, i + 1);
    407. }
    408. for (int i = week_log; i < month_day_score; i++, count++) {
    409. if (month_log == 9 && count == 1) {
    410. label[i].setText(count + "国庆");
    411. label[i].setForeground(Color.red);
    412. riLI[0][i] = String.valueOf(count);
    413. riLI[1][i] = "国庆";
    414. } else if (month_log == 0 && count == 1) {
    415. label[i].setText(count + "元旦");
    416. label[i].setForeground(Color.red);
    417. riLI[0][i] = String.valueOf(count);
    418. riLI[1][i] = "元旦";
    419. } else if (month_log == 11 && count == 24) {
    420. label[i].setText(count + "平安夜");
    421. label[i].setForeground(Color.red);
    422. riLI[0][i] = String.valueOf(count);
    423. riLI[1][i] = "平安夜";
    424. } else if (month_log == 11 && count == 25) {
    425. label[i].setText(count + "圣诞");
    426. label[i].setForeground(Color.red);
    427. riLI[0][i] = String.valueOf(count);
    428. riLI[1][i] = "圣诞";
    429. } else if (month_log == 1 && count == 14) {
    430. label[i].setText(count + "情人节");
    431. label[i].setForeground(Color.red);
    432. riLI[0][i] = String.valueOf(count);
    433. riLI[1][i] = "情人节";
    434. } else if (month_log == 4 && count == 1) {
    435. label[i].setText(count + "劳动节");
    436. label[i].setForeground(Color.red);
    437. riLI[0][i] = String.valueOf(count);
    438. riLI[1][i] = "劳动节";
    439. } else if (LunarDate[i - week_log].equals("春节") || LunarDate[i - week_log].equals("元宵") || LunarDate[i - week_log].equals("端午") || LunarDate[i - week_log].equals("中秋")) {
    440. label[i].setText(count + LunarDate[i - week_log]);
    441. label[i].setForeground(Color.red);
    442. riLI[0][i] = String.valueOf(count);
    443. riLI[1][i] = LunarDate[i - week_log];
    444. } else {
    445. label[i].setText(count + LunarDate[i - week_log]);
    446. riLI[0][i] = String.valueOf(count);
    447. riLI[1][i] = LunarDate[i - week_log];
    448. }
    449. }
    450. System.out.println("=============================================");
    451. System.out.println(year_log + "年" + (month_log + 1) + "月");//输出年月
    452. for (int i = 0; i < 7; i++) {//输出星期
    453. System.out.print(riLI[0][i] + "\t");
    454. }
    455. System.out.println();
    456. for (int i = 1; i < 6; i++) {
    457. for (int j = 7 * i; j < 7 * (i + 1); j++) {
    458. if (null != riLI[0][j]) {//输出公历
    459. System.out.print(riLI[0][j] + "\t");
    460. } else {
    461. System.out.print(" \t");
    462. }
    463. }
    464. System.out.println();
    465. for (int j = 7 * i; j < 7 * (i + 1); j++) {
    466. if (null != riLI[1][j]) {//输出农历
    467. System.out.print(riLI[1][j] + "\t");
    468. } else {
    469. System.out.print(" \t");
    470. }
    471. }
    472. System.out.println();
    473. }
    474. }
    475. public static void main(String[] args) {
    476. JFrame.setDefaultLookAndFeelDecorated(true);
    477. new MainFrame();
    478. }
    479. }

    JAVA 万年历(阳历 阴历 节日 时间) - 图1