import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main extends JFrame implements ActionListener, MouseWheelListener {
private static final long serialVersionUID = 7499985850889594686L;
// 所有的组件都添加到此面板上后添加到jframe 中显示
JPanel parent = null;
JTextField yearField = null;
JTextField monthField = null;
JPanel datePanel = null;
JPanel south = new JPanel();
// 每行七个,其6行
JLabel[] days = new JLabel[6 * 7];
Font f = new Font("Arial", Font.BOLD, 14);
Calendar c = new java.util.GregorianCalendar();
Week week = new Week(c);
Clock2 clock = new Clock2(this);
Lunar lunar = new Lunar(c);
public Main() {
jsInit();
}
private void jsInit() {
this.setTitle("简易万年历for LGC");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
Container c = this.getContentPane();
parent = new JPanel();
datePanel = new JPanel();
datePanel.setBackground(Color.green);
// parent.setBackground(Color.GREEN);
c.add(parent);
for (int i = 0; i < days.length; i++) {
days[i] = new JLabel();
}
// 添加各种组件
addC();
// 添加listener
addLis();
this.setResizable(false);
this.setBounds(60, 60, 350, 280);
this.setVisible(true);
}
private void addLis() {
yearField.addActionListener(this);
monthField.addActionListener(this);
parent.addMouseWheelListener(this);
}
// 添加组件
public void addC() {
// 在其中添加星期信息,并提供一个field 用来输入年月
addInputField();
// 添加日期信息
addDatesPanel();
// 添加农历信息
addTail();
}
private void addDatesPanel() {
datePanel.removeAll();
parent.add(datePanel, BorderLayout.CENTER);
datePanel.setLayout(new GridLayout(6, 7));
if (week.y % 4 == 0 && week.y % 100 != 0)
week.monthday[1] = 29; // 判断是否为闰年
int index = 0;
for (week.d = 1; week.d <= week.monthday[week.m - 1]; week.d++) {
week.w = week.week(week.y, week.m, week.d); // 计算出这个月的开头是星期几
// 根据已算出的其星期几,来输出每个月的一号前的几个空格以使星期与日期对齐
if (week.d == 1) {
for (week.i = 0; week.i < week.w; week.i++) {
setLabelText(index, " /t ");
index++;
}
}
// 开始输出这个月的所有日期
if (week.d <= 31) {/*
* 注:这里的if条件week.d小于的数只要大于等于31就可以,因为到底输出多少天
* 有第二层循环的”d<=week.monthday[week.m-1]“来决定
*/
if (week.d == c.get(Calendar.DAY_OF_MONTH)) {
setLabelText(index,
"<html><font color=red> "
+ week.d + "</font></html> ");
} else {
setLabelText(index, " " + week.d + " ");
}
index++;
}
}
for (; index < 6 * 7; index++) {
setLabelText(index, " /t ");
}
}
private void setLabelText(int index, String text) {
days[index].setText(text);
days[index].setName(week.d + "");
days[index].setFont(f);
int year = Integer.parseInt(yearField.getText().trim());
int month = Integer.parseInt(monthField.getText().trim());
Calendar cc = new GregorianCalendar(year, month - 1, week.d);
String lunDate = new Lunar(cc).toString();
days[index].setToolTipText(lunDate);
datePanel.add(days[index]);
}
private void addWeeksLabels(JPanel p) {
JPanel weeks = new JPanel();
weeks.setBackground(Color.ORANGE);
weeks.setLayout(new GridLayout());
weeks.add(new JLabel("周七 "));
weeks.add(new JLabel("周一 "));
weeks.add(new JLabel("周二 "));
weeks.add(new JLabel("周三 "));
weeks.add(new JLabel("周四 "));
weeks.add(new JLabel("周五 "));
weeks.add(new JLabel("周六 "));
p.add(weeks, BorderLayout.CENTER);
}
public void addInput(JPanel p) {
JPanel inputPanel = new JPanel();
inputPanel.setBackground(Color.YELLOW);
inputPanel.setLayout(new GridLayout(1, 4));
yearField = new JTextField(week.y + "");
monthField = new JTextField(week.m + "");
inputPanel.add(yearField);
inputPanel.add(monthField);
p.add(inputPanel, BorderLayout.NORTH);
}
private void addInputField() {
JPanel par = new JPanel();
parent.add(par);
par.setLayout(new BorderLayout());
addInput(par);
addWeeksLabels(par);
}
public static void main(String[] args) {
new Main();
}
private void modifyDatePanel() {
addDatesPanel();
}
private void modifyModel() {
week.y = Integer.parseInt(yearField.getText().trim());
week.m = Integer.parseInt(monthField.getText().trim());
c.set(Calendar.YEAR, week.y);
c.set(Calendar.MONTH, week.m - 1);
lunar.setCal(c);
}
private void monthAddOne() {
if (week.m < 12) {
week.m++;
} else if (week.m == 12) {
week.y++;
week.m = 1;
}
c.set(Calendar.YEAR, week.y);
c.set(Calendar.MONTH, week.m - 1);
lunar.setCal(c);
}
private void monthMinusOne() {
if (week.m == 1) {
week.y--;
week.m = 12;
} else {
week.m--;
}
c.set(Calendar.YEAR, week.y);
c.set(Calendar.MONTH, week.m - 1);
lunar.setCal(c);
}
private void addTail() {
south.setLayout(new BorderLayout());
parent.remove(south);
parent.add(south, BorderLayout.SOUTH);
addClock(south);
}
private void addClock(JPanel south) {
south.remove(clock);
south.add(clock, BorderLayout.CENTER);
}
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getWheelRotation() == 1) {
monthAddOne();
} else if (e.getWheelRotation() == -1) {
monthMinusOne();
}
yearField.setText(week.y + "");
monthField.setText(week.m + "");
modifyDatePanel();
// addTail();
// this.repaint();
parent.repaint();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == yearField || e.getSource() == monthField) {
try {
modifyModel();
System.out.println("===========");
modifyDatePanel();
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(this, "日期格式不对!!!:"
+ e1.getMessage());
}
parent.repaint();
}
}
}
//============================================
/**
* Clock.java
* Summary 数字时间显示
* @author liyuanxun
*
*/
/**
* java.awt.Canvas:Canvas组件表示屏幕上一个空白矩形区域,应用程序可以在该区域内绘图,或者可以从该区域捕获用户的输入事件。
* java.awt.Font:Font类表示字体,用来以可见方式呈现文本。字体提供将字符序列映射到字形序列所需的信息,以便在Graphics对象和Component对象上呈现字形序列
* java.awt.Graphics类是所有图形上下文的抽象基类,允许应用程序可以在组件(已经在各种设备上实现),以及闭屏图像上,进行绘制
* java.text:提供以与自然语言无关的方式来处理文本,日期,数字和消息的类和接口
* java.text.SimpleDateFormat:是一个以与语言环境相关的方式来格式化和分析日期的额具体类。
*/
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;
class Clock2 extends Canvas implements Runnable {
/**
*
*/
private static final long serialVersionUID = 3660124045489727166L;
Thread t;
String time;
Main frame;
public Clock2(Main frame) {
this.frame = frame;
setSize(350, 100);
setBackground(new Color(204, 232, 207));
t = new Thread(this);
t.start();
}
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("异常");
}
this.repaint(100);
}
}
public void paint(Graphics g) {
Font f = new Font("宋体", Font.BOLD, 13);
SimpleDateFormat SDF = new SimpleDateFormat(
"yyyy'年'MM'月'dd'日' HH:mm:ss");
Calendar now = Calendar.getInstance();
time = SDF.format(now.getTime());
g.setFont(f);
g.setColor(Color.blue);
String lunar = frame.lunar.toString();
String shengxiao = frame.lunar.animalsYear();
g.drawString(time, 20, 10);
g.drawString("农历:", 20, 30);
g.drawString(lunar, 60, 30);
g.drawString("生肖:", 20, 50);
g.drawString(shengxiao, 60, 50);
}
}
//========================================================
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
class Lunar {
private int year;
private int month;
private int day;
private boolean leap;
final static String[] chineseNumber = { "一", "二", "三", "四", "五", "六", "七",
"八", "九", "十", "十一", "十二" };
static SimpleDateFormat chineseDateFormat = new SimpleDateFormat(
"yyyy年MM月dd日");
final static long[] lunarInfo = new long[] { 0x04bd8, 0x04ae0, 0x0a570,
0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0,
0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50,
0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566,
0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0,
0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4,
0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550,
0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950,
0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260,
0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0,
0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40,
0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3,
0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960,
0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0,
0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9,
0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0,
0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65,
0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0,
0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2,
0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0 };
// ==========================传回农历y年的总天数
final private static int yearDays(int y) {
int i, sum = 348;
for (i = 0x8000; i > 0x8; i >>= 1) {
if ((lunarInfo[y - 1900] & i) != 0)
sum += 1;
}
return (sum + leapDays(y));
}
// ====== 传回农历 y年闰月的天数
final private static int leapDays(int y) {
if (leapMonth(y) != 0) {
if ((lunarInfo[y - 1900] & 0x10000) != 0)
return 30;
else
return 29;
} else
return 0;
}
// ====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
final private static int leapMonth(int y) {
return (int) (lunarInfo[y - 1900] & 0xf);
}
// ====== 传回农历 y年m月的总天数
final private static int monthDays(int y, int m) {
if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
return 29;
else
return 30;
}
// ====== 传回农历 y年的生肖
final public String animalsYear() {
final String[] Animals = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇",
"马", "羊", "猴", "鸡", "狗", "猪" };
return Animals[(year - 4) % 12];
}
// ====== 传入 月日的offset 传回干支, 0=甲子
final private static String cyclicalm(int num) {
final String[] Gan = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚",
"辛", "壬", "癸" };
final String[] Zhi = new String[] { "子", "丑", "寅", "卯", "辰", "巳", "午",
"未", "申", "酉", "戌", "亥" };
return (Gan[num % 10] + Zhi[num % 12]);
}
// ====== 传入 offset 传回干支, 0=甲子
final public String cyclical() {
int num = year - 1900 + 36;
return (cyclicalm(num));
}
/** */
/**
* 传出y年m月d日对应的农历. yearCyl3:农历年与1864的相差数 ? monCyl4:从1900年1月31日以来,闰月数
* dayCyl5:与1900年1月31日相差的天数,再加40 ?
*
* @param cal
* @return
*/
public Lunar(Calendar cal) {
setCal(cal);
}
public void setCal(Calendar cal) {
@SuppressWarnings("unused")
int yearCyl, monCyl, dayCyl;
int leapMonth = 0;
Date baseDate = null;
try {
baseDate = chineseDateFormat.parse("1900年1月31日");
} catch (ParseException e) {
e.printStackTrace(); // To change body of catch statement use
// Options | File Templates.
}
// 求出和1900年1月31日相差的天数
int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L);
dayCyl = offset + 40;
monCyl = 14;
// 用offset减去每农历年的天数
// 计算当天是农历第几天
// i最终结果是农历的年份
// offset是当年的第几天
int iYear, daysOfYear = 0;
for (iYear = 1900; iYear < 2050 && offset > 0; iYear++) {
daysOfYear = yearDays(iYear);
offset -= daysOfYear;
monCyl += 12;
}
if (offset < 0) {
offset += daysOfYear;
iYear--;
monCyl -= 12;
}
// 农历年份
year = iYear;
yearCyl = iYear - 1864;
leapMonth = leapMonth(iYear); // 闰哪个月,1-12
leap = false;
// 用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天
int iMonth, daysOfMonth = 0;
for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {
// 闰月
if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) {
--iMonth;
leap = true;
daysOfMonth = leapDays(year);
} else
daysOfMonth = monthDays(year, iMonth);
offset -= daysOfMonth;
// 解除闰月
if (leap && iMonth == (leapMonth + 1))
leap = false;
if (!leap)
monCyl++;
}
// offset为0时,并且刚才计算的月份是闰月,要校正
if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
if (leap) {
leap = false;
} else {
leap = true;
--iMonth;
--monCyl;
}
}
// offset小于0时,也要校正
if (offset < 0) {
offset += daysOfMonth;
--iMonth;
--monCyl;
}
month = iMonth;
day = offset + 1;
}
public static String getChinaDayString(int day) {
String chineseTen[] = { "初", "十", "廿", "卅" };
int n = day % 10 == 0 ? 9 : day % 10 - 1;
if (day > 30)
return "";
if (day == 10)
return "初十";
else
return chineseTen[day / 10] + chineseNumber[n];
}
public String toString() {
return cyclical() + "年" + (leap ? "闰" : "") + chineseNumber[month - 1] + "月"
+ getChinaDayString(day);
}
public static void main(String[] args) throws ParseException {
Calendar today = Calendar.getInstance();
today.setTime(chineseDateFormat.parse("2003年1月1日"));
Lunar lunar = new Lunar(today);
System.out.println("北京时间:" + chineseDateFormat.format(today.getTime())
+ " 农历" + lunar);
}
}
import java.util.Calendar;
//计算星期的函数
/*
* 万年历的公式常用公式: S=X-1+(X-1)/4-(X-1)/100+(X-1)/400+C 说明:X为公元年数;
* C为从元旦起,到要算的那天总天数(如:2003年2月13日,C=31+13=44)
* S/7余数为星期几(如余数为0,1,2,3,4,5,6。分别为星期天,星期一,二,三,四,五,六)
*
* 基姆拉尔森计算公式: 在公式中d表示日期中的日数,m表示月份数,y表示年数。 注意:在公式中有个与其他公式不同的地方:
* 把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13- 10来代入公式计算。
*/
// 用于计算某一日期为星期几
class Week {
int monthday[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int y, w, i, m = 1, d = 1; // y为年、m为月、d为日期、w为星期几
public Week() {
}
public Week(Calendar c) {
y=c.get(Calendar.YEAR);
m=c.get(Calendar.MONDAY)+1;
// d=c.get(Calendar.DAY_OF_MONTH);
}
int week(int y, int m, int d) {
int w;
if ((m == 1) || (m == 2)) {
y--;
m += 12;
}
w = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7; /* 基姆拉尔森计算公式 */
return (w);
}
}