日历记事本程序代码
C 万年历及公历农历转换源代码1
scanf("%d.%d.%d",&gyear,&gmonth,&gday); if(gyear%4==0 && gyear%100!=0 || gyear%400==0) months[2]=29; else months[2]=28; /*以下是计算在该公历年的第几天*/
whatday=gPastday[gmonth]+gday; if((gyear%4==0 && gyear%100!=0 || gyear%400==0) && (gmonth>2)) whatday++; /*上如果是闰年和月份大于二则在该公历年的第几天数+1*/ /*计算该年春节在公历年的第几天*/
clrscr();
}
}
getch();
}
/*公历每月前面的天数*/ const int gPastday[13]={0,0,31,59,90,120,151,181,212,243,273,304,334};
months[13]={0,31,0,31,30,31,30,31,31,30,31,30,31};
{case 1:printf("
* 1 * "); break;
case 3:printf("
* 3 * "); break;
case 5:printf("
* 5 * "); break;
case 7:printf("
* 7 * "); break;
case 9:printf("
* 9 * ");break;
{
万年历c语言源程序
万年历c语言源程序#include stdio.h #include windows.h #include wincon.h #includestring.h #includestdlib.h #includeconio.h #includetime.h #includemath.h #define NULL 0*****_SCREEN_BUFFER_INFO csbiInfo; extern *****API HWND WINAPI GetConsoleWindow(); time_t T;struct tm * now; int min,sec;int hour,nowyear,nowmonth,nowday; void ti2() { time ( T ); now = localtime ( T ); hour=now-tm_hour%12; min=now-tm_min-15; sec=now-tm_sec-15; }void ti(){ time ( T ); now = localtime ( T ); hour=now-tm_hour; min=now-tm_min; sec=now-tm_sec; nowyear=now-tm_year+1900; nowmonth=now-tm_mon+1; nowday=now-tm_mday; }int yearbak=0; void clock_sz(); int pd( int year);void huizs(int line_head_month); void hzdy(int year,int nowmonth_hz); void menu(); void clear();int main(int argc, char* argv[]) { int x=0; ti();clock_sz(); hzdy(nowyear,nowmonth); menu(); return 0; }void clear() { int i; HWND hwnd; HANDLE hOut; HDC hdc;HBRUSH hbr; COORD pos={1,4}; hwnd=GetConsoleWindow(); hdc = GetDC(hwnd); hOut = GetStdHandle(STD_OUTPUT_HANDLE); hbr= CreateSolidBrush(RGB(0,0,0)); SelectObject(hdc,hbr); Rectangle(hdc,0,65,670,470); SetConsoleCursorPosition(hOut, pos); for(i=0;i i++) printf(“ \n "); SetConsoleCursorPosition(hOut, pos); Rectangle(hdc,0,65,670,470); ReleaseDC(hwnd, hdc); } void menu() { int year=0,xyear=0,x, xy=0,i ; struct re { int top; int left; int right; int bottom; }rt; HWND hwnd; HDC hdc,dc; HBRUSH hbr; COORD pos3_1 = {1, 4},pos3_2= {1, 1},pos = {1, 4},pos2_3 = {1, 6},pos2_4 = {1, 7}; INPUT_RECORD mouseRec; DWORD state = 0, res=0; POINT pt; HPEN pn;HANDLE hOut, hIn;hwnd=GetConsoleWindow();//得到控制台窗口句柄hdc = GetDC(hwnd); //得到画图设备句柄dc = GetDC(hwnd);//画刷hbr= CreateSolidBrush(RGB(255,0,0)); SelectObject(dc,hbr);for(rt.left=20,i=2;ii++) rt[i].left=rt[i-1].left+50;for(rt.right=50,i=2;ii++) rt[i].right=rt[i-1].right+50; //定位按钮for(i=0;ii++) rt[i].top=40; for(i=0;ii++) rt[i].bottom=60;hOut = GetStdHandle(STD_OUTPUT_HANDLE); hIn=GetStdHandle(STD_INPUT_HANDLE);pn=CreatePen(PS_SOLID,1,RGB(255,0,255)); GetClientRect(hwnd, rt); SelectObject(hdc, pn);SetBkMode(hdc,*****RENT); MoveToEx(hdc,1,35,NULL); LineTo(hdc,620,35); LineTo(hdc,620,62); LineTo(hdc,1,62); LineTo(hdc,1,35);pn=CreatePen(PS_SOLID,2,RGB(255,255,0));SelectObject(hdc, pn);MoveToEx(hdc,5,75,NULL); LineTo(hdc,45,75);hbr= CreateSolidBrush(RGB(0,0,190)); SelectObject(dc,hbr); for(i=1;ii++) Rectangle(dc,rt[i].left,rt[i].top,rt[i].right,rt[i].bottom);SetTextColor(hdc,RGB(0,255,255));TextOut(hdc,280,10,TEXT("万年历"),6);//输出文本SetTextColor(hdc,RGB(255,255,0));////设颜色TextOut(hdc,rt.left,rt.top,TEXT(" 1"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 2"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 3"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 4"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 5"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 6"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 7"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 8"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 9"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT(" 0"),3);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT("确定"),4);//输出文本TextOut(hdc,rt.left+=50,rt.top,TEXT("清除"),4);//输出文本TextOut(hdc,40,350,TEXT("提示:双击窗口退出"),18);//输出文本SetConsoleCursorPosition(hOut, pos3_1); ShowWindow(hwnd,SW_SHOW); SetConsoleTextAttribute(hOut, 10); for(;;) { SetConsoleTextAttribute(hOut, 10); ReadConsoleInput(hIn, mouseRec, 1, res); Sleep(100); if (mouseRec.EventType == MOUSE_EVENT) { if (mouseRec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK) break; if (mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_*****) { GetCursorPos(pt); ScreenToClient(hwnd,pt); if(pt.x20 pt.x50 pt.yrt.top pt.yrt.bottom) { if(xyear4) printf("1"); xyear++; if(xyear==1) year=1*1000; if(xyear==2) year=year+1*100; if(xyear==3) year=year+1*10; if(xyear==4) { year=year+1; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.top pt.yrt.bottom) { if(xyear4) printf("2"); xyear++; if(xyear==1) year=2*1000; if(xyear==2) year=year+2*100;if(xyear==3) year=year+2*10; if(xyear==4){ year=year+2;printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom) { if(xyear4) printf("3"); xyear++; if(xyear==1) year=3*1000; if(xyear==2) year=year+3*100; if(xyear==3) year=year+3*10; if(xyear==4){ year=year+3; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("4"); xyear++; if(xyear==1) year=4*1000; if(xyear==2) year=year+4*100; if(xyear==3) year=year+4*10; if(xyear==4){ year=year+4; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("5"); xyear++; if(xyear==1) year=5*1000; if(xyear==2) year=year+5*100; if(xyear==3) year=year+5*10; if(xyear==4){ year=year+5;pt.yrt.top pt.yrt.top pt.yrt.topprintf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("6"); xyear++; if(xyear==1) year=6*1000; if(xyear==2) year=year+6*100; if(xyear==3) year=year+6*10; if(xyear==4){ year=year+6; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("7"); xyear++; if(xyear==1) year=7*1000; if(xyear==2) year=year+7*100; if(xyear==3) year=year+7*10;if(xyear==4){ year=year+7; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("8"); xyear++; if(xyear==1) year=8*1000; if(xyear==2) year=year+8*100; if(xyear==3) year=year+8*10; if(xyear==4){ year=year+8; printf("\n你选择的年为%d",year);pt.yrt.top pt.yrt.top pt.yrt.top} continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("9"); xyear++; if(xyear==1) year=9*1000; if(xyear==2) year=year+9*100; if(xyear==3) year=year+9*10; if(xyear==4){ year=year+9; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear4) printf("0"); xyear++; if(xyear==1) year=0*1000; if(xyear==2) year=year+0*100; if(xyear==3) year=year+0*10; if(xyear==4){ year=year+0; printf("\n你选择的年为%d",year); } continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.bottom){ if(xyear=1year!=0) { clear(); yearbak=year; SetConsoleCursorPosition(hOut, pos3_2); printf("公元%d年",yearbak); SetConsoleCursorPosition(hOut, pos3_1); for(i=1;ii+=3) huizs(i);pt.yrt.top pt.yrt.top pt.yrt.topx=MessageBox(NULL,"是否显示后六个月?", "提示",MB_YESNO); if(x==6) { SetConsoleTextAttribute(hOut, 14); clear(); for(i=7;ii+=3) huizs(i); } } else printf("输入不合法,请点击清除重新输入"); continue; } else if(pt.xrt.left pt.xrt.right pt.yrt.top pt.yrt.bottom){ xyear=0; year=0; clear(); clock_sz(); hzdy(nowyear,nowmonth);pn=CreatePen(PS_SOLID,2,RGB(255,255,0)); SelectObject(hdc, pn);//选画笔MoveToEx(hdc,5,75,NULL); LineTo(hdc,45,75); SetConsoleCursorPosition(hOut, pos); continue; } }; } } ReleaseDC(hwnd,hdc); }int pd( int year) { int pr=0; if((year%100==0)(year%400==0)) pr=1; else if(year%4==0year%100!=0) pr=1; else pr=0; return pr; } void huizs(int line_head_month) { int i=0,j=0,k=0,month=0,n=0,daysum=0,jlw=0;int monthhead={0},mlt={1,1,1}; int endline=0,test=0; int year=0; year=yearbak; for(i=1;iyear;i++) { if(pd(i)) daysum+=366; else daysum+=365; } j=daysum%7; monthhead=j; for(month=1;monthmonth++) { if(pd(year)month==2) n=29; else if(pd(year)==0month==2) n=28; else if(month==4||month==6||month==9||month==11) n=30; else n=31; monthhead[month]=(monthhead[month-1]+n)%7; } while(test=5){ for(month=line_head_month;month=(line_head_month+2);mo nth++) { if(month%3==1endline==0) { printf("\t%d月\t\t\t%d月\n",month,month+1,month+2); printf("一二三四五六日一二三四五六日三四五六日\n"); } if(endline==0) { for(k=0;kmonthhead[month-1];k++) printf(" "); j=monthhead[month-1]; } if(endline==0) i=1; else i=mlt[month-1]; for(;ji++,j++) { if(pd(year)month==2) n=29; else if(pd(year)==0month==2) n=28;%d月一二else if(month==4||month==6||month==9||month==11) n=30; else n=31; if(in) { for(k=0;kk++) printf(" "); break; } printf("%-3.0d",i); } printf(" "); mlt[month-1]=i; if(month%3==0) { printf("\n"); endline=1; } j=0; } test++; } }void hzdy(int year,int nowmonth_hz) { int i=0,j=0,k=0,month=0,n=0,daysum=0,jlw=0; int monthhead={0},mlt={1,1,1}; int endline=0,test=0; for(i=1;iyear;i++) { if(pd(i)) daysum+=366; else daysum+=365; } j=daysum%7; monthhead=j; for(month=1;monthmonth++) { if(pd(year)month==2) n=29; else if(pd(year)==0month==2) n=28;//平年else if(month==4||month==6||month==9||month==11) n=30; elsen=31; monthhead[month]=(monthhead[month-1]+n)%7; } if(pd(year)nowmonth_hz==2) n=29;//闰年else if(pd(year)==0nowmonth_hz==2) n=28;//平年else if(nowmonth_hz==4||nowmonth_hz==6||nowmonth_hz==9||no wmonth_hz==11) n=30;//4,6,9,11月else n=31; printf("\n\n\n\n\n\n\n\n 当前时间:%d年%d月%d日%d时%d分\n\n",nowyear,nowmonth,nowday,hour,min); printf("\t一二三四五六日\n\t"); for(k=0;kmonthhead[nowmonth_hz-1];k++) printf(" "); j=monthhead[nowmonth_hz-1]; for(i=1;ii++,j++) { if(j==7) { printf("\n\t"); j=0; } printf("%-3.0d",i); } printf("\n"); } void clock_sz() { float a,b,c; float x=0,y=0,ex=100,ey=160,radian=0,oldx,oldy,radius=90; float mx=0,my=0,oldmx,oldmy,mradius=40; float hx=0,hy=0,oldhx,oldhy,hradius=20; intkd_r1,kd_r2,kd_radius=170,kd_radius1,old_kd_x,old_kd_y,kd_radi an=0,k=0,q=0,oldk,oldq; HWND hwnd; HANDLE hOut; HDC hdc; *****F C1=RGB(0,0,0),C2=RGB(0,0,0); HBRUSH hbr,hbr1,hbr2; HPENhpn,gpn,rpn,ppn2,ypn,bpn,kdpn,rkdpn,dotpn,blue,black,blkdpn, pkdpn,hpn2; hwnd=GetConsoleWindow(); hdc = GetDC(hwnd);hOut = GetStdHandle(STD_OUTPUT_HANDLE); hbr= CreateSolidBrush(RGB(0,100,0)); hbr1= CreateSolidBrush(C1); hbr2= CreateSolidBrush(RGB(0,0,150)); hpn2=CreatePen(PS_SOLID,1,RGB(255,0,0));rpn=CreatePen(PS_SOLID,1,RGB(255,0,0));hpn=CreatePen(PS_SOLID,1,RGB(0,255,255));blue=CreatePen(PS_SOLID,3,RGB(0,0,255));bpn=CreatePen(PS_SOLID,1,RGB(255,255,255));gpn=CreatePen(PS_SOLID,2,RGB(0,255,0));pkdpn=CreatePen(PS_SOLID,3,RGB(0,255,255));kdpn=CreatePen(PS_SOLID,1,RGB(255,255,0));rkdpn=CreatePen(PS_SOLID,4,RGB(255,0,0));blkdpn=CreatePen(PS_SOLID,6,RGB(0,0,0));black=CreatePen(PS_SOLID,4,C1); ex=400;//时钟圆心位置ey=230;while(kd_radian=360){ if(kd_radian%5==0) { if(kd_radian%90==0){ kd_radius=130; SelectObject(hdc,rkdpn); } else{ kd_radius=125; SelectObject(hdc,pkdpn); } } else { kd_radius=130; SelectObject(hdc, kdpn); kd_radius1=120; } y=kd_radius*sin(kd_radian/180.0*3.14);x=kd_radius*cos(kd_radian/180.0*3.14);MoveToEx(hdc,ex,ey,NULL); LineTo(hdc,ex+x,ey+y); y=kd_radius1*sin(kd_radian/180.0*3.14);x=kd_radius1*cos(kd_radian/180.0*3.14); old_kd_x=x; old_kd_y=y; SelectObject(hdc,blkdpn); MoveToEx(hdc,ex,ey,NULL); LineTo(hdc,ex+old_kd_x,ey+old_kd_y); kd_radian+=6; };SetTextColor(hdc,RGB(0,255,0));}TextOut(hdc,ex+kd_radius+8,ey-7,TEXT("3"),1);TextOut(hdc,ex-4,ey+kd_radius+3,TEXT("6"),1); TextOut(hdc,ex-kd_radius-15,ey-5,TEXT("9"),1); TextOut(hdc,ex-7,ey-kd_radius-18,TEXT("12"),2); radius=110;mradius=90;//分针半径hradius=70;//时针半径x=0,y=0; radian=0;SetTextColor(hdc,RGB(0,255,225));////设颜色SetBkColor(hdc,RGB(0,0,0));////设颜色ti2();a=sec*6/180.0*3.14; b=min*6/180.0*3.14;c=(hour*30.0-90.0+(min+15)/2.0)/180.0*3.14; SetTextColor(hdc,RGB(0,255,255)); my=mradius*sin(b); mx=mradius*cos(b); SelectObject(hdc, gpn);MoveToEx(hdc,ex,ey,NULL); LineTo(hdc,ex+mx,ey+my); LineTo(hdc,ex+mx,ey+my); hy=hradius*sin(c); hx=hradius*cos(c);SelectObject(hdc, blue);MoveToEx(hdc,ex,ey,NULL); LineTo(hdc,ex+hx,ey+hy); ReleaseDC(hwnd, hdc);。
Java万年历源代码,可显示公历、农历、系统时间、国际时间
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.table.DefaultTableModel;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Locale;import java.util.TimeZone;public class wannianli extends JFrame implements ActionListener, MouseListener {private Calendar cld = Calendar.getInstance();//获取一个Calendar类的实例对象private String[] astr = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };private DefaultTableModel dtm = new DefaultTableModel(null, astr);private JTable table = new JTable(dtm);private JScrollPane sp = new JScrollPane(table);private JButton bLastYear = new JButton("上一年");private JButton bNextYear = new JButton("下一年");private JButton bLastMonth = new JButton("上月");private JButton bNextMonth = new JButton("下月");private JPanel p1 = new JPanel(); // 设立八个中间容器,装入布局控制日期的按钮模块private JPanel p2 = new JPanel(new GridLayout(3,2));//网格布局private JPanel p3 = new JPanel(new BorderLayout());//边界布局private JPanel p4 = new JPanel(new GridLayout(2,1));private JPanel p5 = new JPanel(new BorderLayout());private JPanel p6 = new JPanel(new GridLayout(2,2));private JPanel p7 = new JPanel(new GridLayout(2,1));private JPanel p8 = new JPanel(new BorderLayout());private JComboBox timeBox = newJComboBox(TimeZone.getAvailableIDs());//对所有支持时区进行迭代,获取所有的id;private JTextField jtfYear = new JTextField(5);// jtfYeaar年份显示输入框private JTextField jtfMonth = new JTextField(2);// jtfMouth月份显示输入框private JTextField timeField=new JTextField();//各城市时间显示框private static JTextArea jta = new JTextArea(10,5);//农历显示区private JScrollPane jsp = new JScrollPane(jta);private JLabel l = new JLabel("花江小精灵:亲!你可以直接输入年月查询.");private JLabel lt = new JLabel();private JLabel ld = new JLabel();private JLabel lu = new JLabel("农历和节气");private JLabel null1=new JLabel();private int lastTime;//private String localTime = null;private String s = null;private SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy年MM月dd 日 hh时mm分ss秒");public wannianli() {super("花江日历过去仅留追忆,未来刚生憧憬,唯有坚守本心,把握今天 ZYT 詹永堂 ");// 框架命名this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 窗口关闭函数this.getContentPane().setLayout(new BorderLayout(9, 10));jta.setLineWrap(true);// 长度大于分配长度时候则换行jta.setFont(new Font("黑体", Font.BOLD, 16));table.setBackground(Color.white);table.setGridColor(Color.pink);// 星期之间的网格线是灰色的table.setBackground(Color.white);table.setColumnSelectionAllowed(true);// 将table中的列设置为可选择的table.setSelectionBackground(Color.pink);// 当选定某一天时背景颜色为黑色table.setSelectionForeground(Color.GREEN);table.setBackground(new Color(184,207, 229));// 日期显示表格为浅蓝色table.setFont(new Font("黑体", Font.BOLD, 24));// 日期数字字体格式table.setRowHeight(26);// 表格的高度table.addMouseListener(this); // 鼠标监听器、lu.setFont(new Font("黑体", Font.BOLD, 22));//农历标签格氏jtfYear.addActionListener(this);// 可输入年份的文本框// 为各个按钮添加监听函数bLastYear.addActionListener(this);bNextYear.addActionListener(this);bLastMonth.addActionListener(this);bNextMonth.addActionListener(this);timeBox.addItemListener(new TimeSelectedChangedListener());// 将按钮添加到Jpane上p1.add(bLastYear);p1.add(jtfYear);// 年份输入文本框p1.add(bNextYear);p1.add(bLastMonth);p1.add(jtfMonth);p1.add(bNextMonth);p3.add(jsp, BorderLayout.SOUTH);p3.add(lu,BorderLayout.CENTER);p3.add(ld, BorderLayout.NORTH);p4.add(lt);p4.add(l);p5.add(p4, BorderLayout.SOUTH);p5.add(sp, BorderLayout.CENTER);p5.add(p1, BorderLayout.NORTH);p6.add(timeBox);p6.add(null1);p6.add(timeField);p8.add(p2,BorderLayout.CENTER);p8.add(p7,BorderLayout.SOUTH);this.getContentPane().add(p3, BorderLayout.EAST);this.getContentPane().add(p5, BorderLayout.CENTER);this.getContentPane().add(p6,BorderLayout.SOUTH);this.getContentPane().add(p8,BorderLayout.WEST);String[] strDate = DateFormat.getDateInstance().format(new Date()) .split("-");// 获取日期cld.set(Integer.parseInt(strDate[0]), Integer.parseInt(strDate[1]) - 1,0);showCalendar(Integer.parseInt(strDate[0]),Integer.parseInt(strDate[1]), cld);jtfMonth.setEditable(false);// 设置月份文本框为不可编辑jtfYear.setText(strDate[0]);jtfMonth.setText(strDate[1]);this.showTextArea(strDate[2]);ld.setFont(new Font("新宋体", Font.BOLD, 24));new Timer(lt).start();new TimeThread().start();this.setBounds(200, 200, 700, 350);this.setResizable(false);this.setVisible(true);}public void showCalendar(int localYear, int localMonth, Calendar cld) {int Days = getDaysOfMonth(localYear, localMonth) +cld.get(Calendar.DAY_OF_WEEK) -2;Object [] ai = new Object[7];lastTime = 0;for (int i = cld.get(Calendar.DAY_OF_WEEK)-1; i <= Days; i++) {ai[i%7] =String.valueOf(i-(cld.get(Calendar.DAY_OF_WEEK)-2));if (i%7 == 6){dtm.addRow(ai);ai = new Object[7];lastTime++;}}dtm.addRow(ai);}public int getDaysOfMonth(int Year, int Month) {//计算各月的天数if(Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Mont h==12){return 31;}if(Month==4||Month==6||Month==9||Month==11){return 30;}if(Year%4==0&&Year%100!=0||Year%400==0)//闰年{return 29;}else {return 28;}}public void actionPerformed(ActionEvent e)//从界面上获取年月数据{if(e.getSource() == jtfYear || e.getSource() == bLastYear || e.getSource() == bNextYear ||e.getSource() == bLastMonth || e.getSource() == bNextMonth){int m, y;try//控制输入的年份正确,异常控制{if (jtfYear.getText().length() != 4){throw new NumberFormatException();}y = Integer.parseInt(jtfYear.getText());m = Integer.parseInt(jtfMonth.getText());}catch (NumberFormatException ex){JOptionPane.showMessageDialog(this, "请输入4位0-9的数字!", "年份有误", JOptionPane.ERROR_MESSAGE);return;}ld.setText("没有选择日期");for (int i = 0; i < lastTime+1; i++){ dtm.removeRow(0);}if(e.getSource() ==bLastYear){ jtfYear.setText(String.valueOf(--y)); }if(e.getSource() ==bNextYear){jtfYear.setText(String.valueOf(++y)); }if(e.getSource() == bLastMonth){if(m == 1){jtfYear.setText(String.valueOf(--y));m = 12;jtfMonth.setText(String.valueOf(m));}else{jtfMonth.setText(String.valueOf(--m));}}if(e.getSource() == bNextMonth){if(m == 12){jtfYear.setText(String.valueOf(++y));m = 1;jtfMonth.setText(String.valueOf(m));}else{jtfMonth.setText(String.valueOf(++m));}}cld.set(y, m-1, 0);showCalendar(y, m, cld);}}public void mouseClicked(MouseEvent e){jta.setText(null);int r = table.getSelectedRow();int c = table.getSelectedColumn();if (table.getValueAt(r,c) == null){ld.setText("没有选择日期");}else{this.showTextArea(table.getValueAt(r,c));}}private void showTextArea(Object selected){ld.setText(jtfYear.getText()+"年"+jtfMonth.getText()+"月"+selected+"日");}public static void main(String[] args){JFrame.setDefaultLookAndFeelDecorated(true);JDialog.setDefaultLookAndFeelDecorated(true);new wannianli();jta.setText(today());}private void updateTimeText(String timeZoneId) {if(timeZoneId != null){TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);dateFormat.setTimeZone(timeZone);Calendar calendar = Calendar.getInstance();calendar.setTimeZone(timeZone);timeField.setText(dateFormat.format(calendar.getTime()));}else{timeField.setText(null);}}private class TimeSelectedChangedListener implements ItemListener { public void itemStateChanged(ItemEvent e) {if (e.getStateChange()==ItemEvent.SELECTED) {if (e.getItem() instanceof String) {s = e.getItem().toString();}}}}private class TimeThread extends Thread{public void run(){while(true){updateTimeText(s);try{Thread.sleep(100);}catch(InterruptedException e){e.printStackTrace();}}}}class Timer extends Thread //显示系统时间{private JLabel lt;private SimpleDateFormat fy = new SimpleDateFormat(" Gyyyy.MM.dd HH:mm:ss ");public Timer(JLabel lt){this.lt=lt;}public void run(){while(true){try{lt.setText(fy.format(new Date()));this.sleep(500);}catch(InterruptedException ex){ex.printStackTrace();}}}}final private 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 };final private static int[] year20 = new int[] { 1, 4, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1 };final private static int[] year19 = new int[] { 0, 3, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 };final private static int[] year2000 = new int[] { 0, 3, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1 };public final static String[] nStr1 = new String[] { "", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一","十二" };private final static String[] Gan = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };private final static String[] Zhi = new String[] { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };private final static String[] Animals = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };// 传回农历 y年的总天数final private static int lYearDays(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;elsereturn 29;} elsereturn 0;}// 传回农历 y年闰哪个月 1-12 , 没闰传回 0final 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;elsereturn 30;}// 传回农历 y年的生肖final public static String AnimalsYear(int y) {return Animals[(y - 4) % 12];}//传入月日的offset 传回干支,0=甲子final private static String cyclicalm(int num) {return (Gan[num % 10] + Zhi[num % 12]);}// 传入 offset 传回干支, 0=甲子final public static String cyclical(int y) {int num = y - 1900 + 36;return (cyclicalm(num));}// 传出农历.year0 .month1 .day2 .yearCyl3 .monCyl4 .dayCyl5 .isLeap6final private long[] Lunar(int y, int m) {long[] nongDate = new long[7];int i = 0, temp = 0, leap = 0;Date baseDate = new GregorianCalendar(1900 + 1900, 1,31).getTime();Date objDate = new GregorianCalendar(y + 1900, m, 1).getTime();long offset = (objDate.getTime() - baseDate.getTime()) / 86400000L;if (y < 2000)offset += year19[m - 1];if (y > 2000)offset += year20[m - 1];if (y == 2000)offset += year2000[m - 1];nongDate[5] = offset + 40;nongDate[4] = 14;for (i = 1900; i < 2050 && offset > 0; i++) {temp = lYearDays(i);offset -= temp;nongDate[4] += 12;}if (offset < 0) {offset += temp;i--;nongDate[4] -= 12;}nongDate[0] = i;nongDate[3] = i - 1864;leap = leapMonth(i); // 闰哪个月nongDate[6] = 0;for (i = 1; i < 13 && offset > 0; i++) {// 闰月if (leap > 0 && i == (leap + 1) && nongDate[6] == 0) { --i;nongDate[6] = 1;temp = leapDays((int) nongDate[0]);} else {temp = monthDays((int) nongDate[0], i);}// 解除闰月if (nongDate[6] == 1 && i == (leap + 1))nongDate[6] = 0;offset -= temp;if (nongDate[6] == 0)nongDate[4]++;}if (offset == 0 && leap > 0 && i == leap + 1) {if (nongDate[6] == 1) {nongDate[6] = 0;} else {nongDate[6] = 1;--i;--nongDate[4];}}if (offset < 0) {offset += temp;--i;--nongDate[4];}nongDate[1] = i;nongDate[2] = offset + 1;return nongDate;}// 传出y年m月d日对应的农历.year0 .month1 .day2 .yearCyl3 .monCyl4 .dayCyl5 .isLeap6final public static long[] calElement(int y, int m, int d) {long[] nongDate = new long[7];int i = 0, temp = 0, leap = 0;Date baseDate = new GregorianCalendar(0 + 1900, 0, 31).getTime();Date objDate = new GregorianCalendar(y, m - 1, d).getTime();long offset = (objDate.getTime() - baseDate.getTime()) / 86400000L;nongDate[5] = offset + 40;nongDate[4] = 14;for (i = 1900; i < 2050 && offset > 0; i++) {temp = lYearDays(i);offset -= temp;nongDate[4] += 12;}if (offset < 0) {offset += temp;i--;nongDate[4] -= 12;}nongDate[0] = i;nongDate[3] = i - 1864;leap = leapMonth(i); // 闰哪个月nongDate[6] = 0;for (i = 1; i < 13 && offset > 0; i++) {// 闰月if (leap > 0 && i == (leap + 1) && nongDate[6] == 0) { --i;nongDate[6] = 1;temp = leapDays((int) nongDate[0]);} else {temp = monthDays((int) nongDate[0], i);}// 解除闰月if (nongDate[6] == 1 && i == (leap + 1))nongDate[6] = 0;offset -= temp;if (nongDate[6] == 0)nongDate[4]++;}if (offset == 0 && leap > 0 && i == leap + 1) { if (nongDate[6] == 1) {nongDate[6] = 0;} else {nongDate[6] = 1;--i;--nongDate[4];}}if (offset < 0) {offset += temp;--i;--nongDate[4];}nongDate[1] = i;nongDate[2] = offset + 1;return nongDate;}public final static String getChinaDate(int day) { String a = "";if (day == 10)return"初十";if (day == 20)return"二十";if (day == 30)return"三十";int two = (int) ((day) / 10);if (two == 0)a = "初";if (two == 1)a = "十";if (two == 2)a = "廿";if (two == 3)a = "三";int one = (int) (day % 10);switch (one) {case 1:a += "一";break;case 2:a += "二";break;case 3:a += "三";break;case 4:a += "四";break;case 5:a += "五";break;case 6:a += "六";break;case 7:a += "七";break;case 8:a += "八";break;case 9:a += "九";break;}return a;}public static String today() {Calendar today = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);int year = today.get(Calendar.YEAR);int month = today.get(Calendar.MONTH) + 1;int date = today.get(Calendar.DATE);long[] l = calElement(year, month, date);StringBuffer sToday = new StringBuffer();try {sToday.append(sdf.format(today.getTime()));sToday.append(" \n");sToday.append(" \n");sToday.append(" \n");sToday.append(" 农历");sToday.append(cyclical(year));sToday.append('(');sToday.append(AnimalsYear(year));sToday.append(")年");sToday.append(" \n");sToday.append(" ");sToday.append(nStr1[(int) l[1]]);sToday.append("月");sToday.append(getChinaDate((int) (l[2])));return sToday.toString();} finally {sToday = null;}}private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年M月d日 EEEEE");public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) { }}。
C语言实训--万年历源代码
C语言实训--万年历源代码# include#include#include#include#include# define YEAR 365int numday[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31}, {31,29,31,30,31,30,31,31,30,31,30,31}};int leap(int year);int calday(int y,int m);int counterdays(int year,int month,int day);int week(int year,int month,int day);int eachyeartotal(int year,int month,int day);int maxDay(int y,int month);void print(int y,int m,int w);void festival (int year,int month,int day);int IsLeap(int y);int guanli(int i);void rili(int year,int month);int rili1();void rili2();void rili3();void rili4();void rili5();void fan();void show(int year,int month,int date,int sec,int mini,int hour);int main(){int i;printf("***************************************\n"); printf("1.查询日历,输入年份:\n");printf("2.查询该月日历及对应星期,输入年份和月份:\n"); printf("3.查询星期几及节日,输入年月日:\n");printf("4.距您输入日期还有多少天,输入日期:\n"); printf("5.通过键盘上下控制不同年月日:\n");scanf("%d",&i);guanli(i);return 0;}int guanli(int i){switch(i){case 1:rili1();return main();case 2:rili2();return main();case 3:rili3();return main();case 4:rili4();return main();case 5:rili5();return main();}return 0;}int rili1(){int year;int n,i;printf("输入年份:");scanf("%d",&year);printf("the calendar of the year%d\n",year);printf("\n");printf("=============================\n"); for(i=1;i<13;i++){printf(" %d月份\n",i);rili(year,i);}n = leap(year);return 0;}void rili2(){int year,month,w;printf("输入年月:");scanf("%d%d",&year,&month);w=calday(year,month);print(year,month,w);}void rili3(){int year,month,day,number;printf("输入年月日:");scanf("%d%d%d",&year,&month,&day);festival(year,month,day);number= week(year,month,day);printf("星期%d",number);}void rili4(){int year,month,day;printf("输入年月日:");scanf("%d%d%d",&year,&month,&day); eachyeartotal( year,month,day);counterdays(year,month,day);}void rili5(){int year,month,date,sec,mini,hour;fan();show(year,month,date,sec,mini,hour);}int leap(int year){int n=(year%4==0 && year%100!=0 || year%100!=0); return n;}void rili(int year,int month){int i,day,first_day,flag,year1,year2;int temp = 6;flag =leap(year);year1=year/100;year2=year%100;if(year>2000){year1=year1-20;day = year1/4;day+=(YEAR+24)*year1+YEAR*year2+(year2-1)/4;if(year>2000)day++;switch(month){case 1:day; break;case 2:day+=31;break;case 3:day+=59+flag;break;case 4:day+=90+flag;break;case 5:day+=120+flag;break;case 6:day+=151+flag;break;case 7:day+=181+flag;break;case 8:day+=212+flag;break;case 9:day+=243+flag;break;case 10:day+=273+flag;break;case 11:day+=304+flag;break;case 12:day+=334+flag;break;}day+=temp;first_day=day%7;}else{year1 = 20-1-year;year2=100-year2-1;day+=(YEAR+24)*year1+YEAR*year2+year2/4; switch(month){case 12:day+=31;break;case 11:day+=61;break;case 10:day+=92;break;case 9:day+=122;break;case 8:day+=153;break;case 7:day+=184;break; case 6:day+=214;break; case 5:day+=245;break; case 4:day+=275;break; case 3:day+=306;break; case 2:day+=334+flag;break; case 1:day+=365+flag;break; }day-=temp;day = day%7;first_day=(7-day)%7;}switch(month){case 1:case 2:month=28+flag;break; case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:month=30;break; case 12:month =31;break;}for(i=1;i<4*first_day;i++) printf(" ");for(i=0;i<month;i++)< p="">{printf("%4d",i+1);if((first_day+i)%7==6)printf("\n");}printf("\n");printf("===============================\n");}///////////////////////////////////////////////////////////int counterdays(int year,int month,int day){int year1,days0,days1,month1,days,day1;time_t timep;struct tm*p;time(&timep);p=gmtime(&timep);year1=(1900+p->tm_year);month1=(1+p->tm_mon);day1=(p->tm_mday);days0=(year-1)*365 + (year-1)/4 - (year-1)/100 + (year-1)/400 +eachyeartotal (year,month,day);days1=(year1 - 1)*365+(year1-1)/4-(year1-1)/100 + (year1 - 1)/400 +eachyeartotal (year1,month1,day1);days=days0-days1;printf("\n今天距%d年%d月%d日还有%d天\n\n",year,month,day,days);return days;}int eachyeartotal(int year,int month,int day){int i,sum=0;for(i=1;i<month;i++)< p="">{sum+=maxDay(year,i);}sum+=day;return sum;}int IsLeap(int y){if(y%4==0 && y%100!=0 || y%400==0) return 1; else return 0;}int maxDay(int y,int month){int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};if(IsLeap(y))a[1]=29;return a[month-1];}///////////////////////////////////////void festival (int year,int month,int day){if (month==1 && day==1) printf (",这天是元旦");if (month==2 && day==14) printf (",这天是情人节"); if (month == 3)switch(day){case 8:printf (",这天是妇女节"); break;case 12:printf (",这天是植树节");}if (month==1 && day==1) printf (",这天是愚人节");if(month == 5)switch (day){case 1:printf(",这天是劳动节");break;case 4:printf(",这天是青年节");}if (month==6 && day==1) printf (",这天是儿童节");if (month==7 && day==1) printf (",这天是建党节");if (month==8 && day==1) printf (",这天是建军节");if (month==9 && day==10) printf (",这天是教师节");if (month==10 && day==1) printf (",这天是国庆节");printf("\n");}int week(int year,int month,int day){int number = (year-1)+(year-1)/4 - (year-1)/100+(year-1)/400+eachyeartotal(year,month,day);number = number%7;return number;}/////////////////////////////////////////////void print(int y,int m,int w){int i,l;printf("Calendar %d-%02d\n",y,m);printf("---------------------\n");printf("Su Mo Tu We Th Fr Sa \n");printf("---------------------\n");for (i=0;i<w;i++)< p=""> printf(" ");if (IsLeap(y))l=1;elsel=0;for (i=1;i<=numday[l][m-1];i++) {printf("%-3d",i);w++;if (w%7==0)printf("\n");}if (w%7) printf("\n");printf("---------------------\n"); }int calday(int y,int m){int d=0,w;while (y>1910){if (IsLeap(y)) d+=366;else d+=365;y--;}while (m>1){ --m;d+=numday[0][m-1];}w=(d+1)%7;return w;}//////////////////////////////////////////void show(int year,int month,int date,int sec,int mini,int hour) {const char month_str[][4]={"","Jan","Feb","Mar","Apl", "May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};const int month_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};int i,j,wdays,mdays,days;for(i=1,days=0;i<year;i++)< p="">{if(leap(i))days += 366;}else{days += 365;}}for(i=1;i<month;i++)< p="">{if(i==2 && leap(year)){days+=29;}else{days+=month_day[i];}}printf(" %s.%d.%d %d:%d:%d \n",month_str[month],date,year,hour,mini,sec);printf(" 一二三四五六日\n");wdays = days % 7;for( j = 0; j < wdays; j++){printf(" ");}if(month == 2 && leap(year)){mdays=29;}else{mdays= month_day[month];}for(i=1;i<=mdays;i++){if( i > 1 && days % 7 == 0 ){printf("\n");}printf("%4d",i);days=days+1; }printf("\n---------------------------\n\n\n");}{time_t rawtime;struct tm *info;int year,month,date,sec,mini,hour; char ch;time ( &rawtime );info = localtime ( &rawtime );year =info->tm_year + 1900; month =info->tm_mon + 1;date =info->tm_mday;hour =info->tm_hour;mini =info->tm_min;sec =info->tm_sec;while(1){show(year,month,date,sec,mini,hour); printf("↑......上一年\n\n");printf("↓......下一年\n\n");printf("←....前一个月\n\n");pr intf("→....后一个月\n\n");printf("Esc.......退出\n\n");ch=getch();switch(ch){case 27://Ecs exit(0);case -32://Navigatorch=getch();if(ch==77){//Rightyear+=(month==12)?1:0;month=month%12+1;}else if(ch==75){year-=(month==1)?1:0; month=(month-2+12)%12+1; }else if(ch==72)year--;else if(ch==80)year++;system("cls");}}</month;i++)<></year;i++)<></w;i++)<></month;i++)<></month;i++)<>。
打印日历每行三个月份源代码
case 5:days = 120 + day;break;
case 6:days = 151 + day;break;
case 7:days = 181 + day;break;
case 8:days = 212 + day;break;
case 9:days = 243 + day;break;
break;
case 4:
mfOfWeek = zhouji(year,10, 1);
mfDays = 31;
msOfWeek = zhouji(year, 11, 1);
msDays = 30;
mtOfWeek = zhouji(year, 12, 1);
mtDays = 31;
PrintOneQuarter(year, mfOfWeek, mfDays, msOfWeek, msDays, mtOfWeek, mtDays, hang);
{
if (month > 2)
{
++days;
}
}
return days;
}
void PrintOneQuarter(int year, int mfOfWeek, int mfDays, int msOfWeek,
int msDays, int mtOfWeek, int mtDays, int hang)
int msOfWeek, int msDays, int mtOfWeek, int mtDays, int hang);
int zhouji(int year, int month, int day);
日历源代码——java
日历源代码——java//import java.sql.Date;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Locale;public class GregorianCalendar日历 {public static void main(String[] args) { //设置不同地区Locale.setDefault();//创建当前日历对象GregorianCalendar now = new GregorianCalendar();//从当前时期对象是取出时间日期对象//编辑错误:Type mismatch: cannot convert from java.util.Date to java.sql.DateDate date = now.getTime();//将时间日期对象按字符形式打印System.out.println(date.toString());//重新将时间对象设置到日期对象中now.setTime(date);//从当前日期对象是取出当前月份、日期int today =now.get(Calendar.DAY_OF_MONTH);int month = now.get(Calendar.MONTH);//获取本月开始日期now.set(Calendar.DAY_OF_MONTH, 1);//获取本月开始日期在一周中的编号int week = now.get(Calendar.DAY_OF_WEEK);//打印日历头并换行设置当前月中第一天的开始位置System.out.println("星期日星期一星期二星期三星期四星期五星期六");//设置当前月中第一天的开始位置for( int i = Calendar.SUNDAY; i < week; i++){ System.out.print(" ");//按规格打印当前月的日期数字while(now.get(Calendar.MONTH) ==month){//取出当前日期int day =now.get(Calendar.DAY_OF_MONTH);//设置日期数字小于10与不小于10两种情况的打印规格if(day < 10){//设置当前日期的表现形式if(day == today)System.out.print(" <" + day + "> ");elseSystem.out.print(" " + day + " ");}else{//设置当前日期的表现形式if(day == today)System.out.print(" <" + day + "> ");elseSystem.out.print(" " + day + " ");}//设置什么时候换行if(week == Calendar.SATURDAY)System.out.println();//设置日期与星期几为下一天now.add(Calendar.DAY_OF_MONTH, 1);week = now.get(Calendar.DAY_OF_WEEK);}}}}。
前端开发实训案例教程初级开发简单的在线日历应用
前端开发实训案例教程初级开发简单的在线日历应用前端开发实训案例教程:初级开发简单的在线日历应用在本篇教程中,我们将学习如何利用前端开发技术创建一个简单的在线日历应用。
在线日历应用是一种常见的实际应用程序,它可以帮助用户记录和组织重要的日期和事件。
我们将使用HTML、CSS和JavaScript来实现这个日历应用。
1. 搭建基本框架我们首先创建一个基本的HTML文件。
在文件中,我们添加一个标题,一个容器用于显示日历,并引入所需的CSS和JavaScript文件。
```html<!DOCTYPE html><html><head><title>简单的在线日历应用</title><link rel="stylesheet" type="text/css" href="style.css"></head><body><h1>简单的在线日历应用</h1><div id="calendar"></div><script src="script.js"></script></body></html>```2. CSS样式接下来,我们需要为日历应用添加CSS样式。
创建一个名为`style.css`的CSS文件,并添加以下代码:```css#calendar {width: 300px;border: 1px solid #ccc;padding: 10px;}#calendar table {width: 100%;}#calendar th {background-color: #ccc;}#calendar td {text-align: center;padding: 5px;}#calendar .today {background-color: #f00;color: #fff;}#calendar .selected {background-color: #0f0;}```3. 实现日历功能现在我们开始使用JavaScript编写日历应用的逻辑。
日历代码_精品文档
日历代码
1. 简介
日历是用来显示日期的一种工具。
在计算机编程中,我们可以通过编写代码来生成和显示日历。
本文将介绍如何用Python编写一个简单的日历代码。
我们将使用Python的datetime和calendar模块来实现这个功能。
2. 实现步骤
2.1 导入模块
我们首先需要导入Python的datetime和calendar模块。
datetime模块提供了日期和时间的相关功能,而calendar模块则提供了生成日历的功能。
import datetime
import calendar
2.2 获取当前日期
我们可以使用datetime模块的date类来获取当前日期。
today = datetime.date.today()
2.3 获取当前月份的日历
我们可以使用calendar模块的monthcalendar函数来生成当前月份的日历。
cal = calendar.monthcalendar(today.year, today.month)
2.4 打印日历
我们可以使用循环遍历生成的日历列表,并打印出每个日期。
```python for week in cal: for day in week: if day == 0: print(。
C语言万年历代码
else if(ch=='n'||ch=='N')
{ month+=1;
if(month>12) {month=1;year+=1;}
}
printf("\t\t********e退出 p上一月 n下一月********");
}
void first_set(int *year,int *month)
{ do{
printf ("please input the year(1~9999):");
if (choose=='y'||choose=='Y') main();
}
days_of_month(int x,int y)
{
int z;
switch (y)
{case 1:
case 3:
case 5:
case 7:
case 8:
printf ("please input the month(1~12):");
scanf ("%d",month);
if(*month<1||*month>12) printf ("WANNING:ERROR,please input again!\n");
printf ("\n\n********************************************************************************\n\n");
自己做的日历(C语言)
本帖最后由 kekeyu 于 2010-8-15 16:59 编辑
网页显示月份、日期、星期、时间代码
网页显示月份、日期、星期、时间代码一、网页显示月份、日期、星期代码<script language=JavaScript>today=new Date();function initArray(){this.length=initArray.arguments.lengthfor(var i=0;i<this.length;i++)this[i+1]=initArray.arguments[i] }var d=new initArray("星期日","星期一","星期二","星期三","星期四","星期五","星期六");document.write("<font color=##000000 style='font-size:9pt;font-family: 宋体'> ",today.getYear(),"年",today.getMonth()+1,"月",today.getDate(),"日",d[today.getDay()+1],"</font>" );</script>二、六种风格时间显示,一定有你喜欢的!<SCRIPT language="javascript"><!--function initArray(){for(i=0;i<initArray.arguments.length;i++)this[i]=initArray.arguments[i];}var isnMonths=new initArray("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");var isnDays=new initArray("星期日","星期一","星期二","星期三","星期四","星期五","星期六","星期日");today=new Date();hrs=today.getHours();min=today.getMinutes();sec=today.getSeconds();clckh=""+((hrs>12)?hrs-12:hrs);clckm=((min<10)?"0":"")+min;clcks=((sec<10)?"0":"")+sec;clck=(hrs>=12)?"下午":"上午";var stnr="";var ns="0123456789";var a="";function getFullYear(d){yr=d.getYear();if(yr<1000)yr+=1900;return yr;}document.write("<table>");//下面各行分别是一种风格,把不需要的删掉即可document.write("<TR><TD>风格一:</TD><TD>"+isnDays[today.getDay()]+","+isnMonths[today. getMonth()]+""+today.getDate()+"日,"+getFullYear(today)+"年");document.write("<TR><TD>风格二:</TD><TD>"+clckh+":"+clckm+":"+clcks+""+clck+"</TD></TR>");document.write("<TR><TD>风格三:</TD><TD>"+isnDays[today.getDay()]+","+isnMonths[today. getMonth()]+""+today.getDate()+"日,"+getFullYear(today)+"年"+clckh+":"+clckm+":"+clcks+""+clck+"</TD></TR>");document.write("<TR><TD>风格四:</TD><TD>"+(today.getMonth()+1)+"/"+today.getDate()+"/ "+(getFullYear(today)+"").substring(2,4)+"</TD></TR>");document.write("<TR><TD>风格五:</TD><TD>"+hrs+":"+clckm+":"+clcks+"</TD></TR>");document.write("<TR><TD VALIGN=TOP>风格六:</TD><TD>"+today+"</TD></TR>");document.write("</table>");//--></SCRIPT>三、这个时钟是有影子的,而且还在不停地走着呢<div id="bgclockshade" style="position:absolute;visibility:visible;font-family:'Arialblack';color:#cccccc;font-size:20px;top:50px;left:173px"></div> <div id="bgclocknoshade" style="position:absolute;visibility:visible;font-family:'Arialblack';color:#000000;font-size:20px;top:48px;left:170px"></div> <div id="mainbody" style="position:absolute; visibility:visible"></div><script language=javaScript><!--function www_helpor_net() {thistime= new Date()var hours=thistime.getHours()var minutes=thistime.getMinutes()var seconds=thistime.getSeconds()if (eval(hours) <10) {hours="0"+hours}if (eval(minutes) < 10) {minutes="0"+minutes}if (seconds < 10) {seconds="0"+seconds}thistime = hours+":"+minutes+":"+secondsif(document.all) {bgclocknoshade.innerHTML=thistimebgclockshade.innerHTML=thistime}if(yers) {document.bgclockshade.document.write('<divid="bgclockshade"style="position:absolute;visibility:visible;font-family:Verdana;color:FFAAAAA;font-size:20px;top:10px;left:152px">'+thistime+'</div>')document.bgclocknoshade.document.write('<divid="bgclocknoshade"style="position:absolute;visibility:visible;font-family:Verdana;color:DDDDDD;font-size:20px;top:8px;left:150px">'+thistime+'</div>')document.close()}var timer=setTimeout("www_helpor_net()",200)}www_helpor_net();//--></script>四、数字化的时钟<span id="liveclock" style"=width: 109px; height:15px"></span><SCRIPT language=javascript>function www_helpor_net(){var Digital=new Date()var hours=Digital.getHours()var minutes=Digital.getMinutes()var seconds=Digital.getSeconds()if(minutes<=9)minutes="0"+minutesif(seconds<=9)seconds="0"+secondsmyclock="现在时刻:<font size='5' face='Arial black'>"+hours+":"+minutes+":"+seconds+"</font>"if(yers){yers.liveclock.document.wri te(myclock)yers.liveclock.document.close()}else if(document.all)liveclock.innerHTML=myclocksetTimeout("www_helpor_net()",1000)}www_helpor_net();//--></SCRIPT>五、动态时钟代码2,此代码相当简单<SCRIPT>setInterval("jnkc.innerHTML=newDate().toLocaleString()+' 星期'+'日一二三四五六'.charAt (new Date().getDay());",1000);</SCRIPT>六、flash时钟,改变sz1.swf中的1为0、1、2、3、4、5、6,可以获得不同样式的时钟,你试试吧。
java万年历程序代码
package pack;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Calendar;import java.util.Date;public class rili extends JFrame implements ActionListener { JButton b_today, b_query;JLabel lb_Year, lb_Month;JButton b_week[] = new JButton[7];JButton b_day[][] = new JButton[6][7];Container thisContainer;JPanel pUp;JPanel pCenter;JPanel pCenter_week, pCenter_day;JComboBox year, month;public void init() {b_today = new JButton("Today");b_query = new JButton("Query");setTitle("日历");lb_Year = new JLabel("Year");lb_Month = new JLabel("Month");year = new JComboBox();month = new JComboBox();setDate();pUp = new JPanel();pUp.add(lb_Year);pUp.add(year);pUp.add(lb_Month);pUp.add(month);pUp.add(b_today);pUp.add(b_query);b_today.addActionListener(this);b_query.addActionListener(this);pCenter = new JPanel();pCenter_week = new JPanel();b_week[0] = new JButton("星期日");b_week[1] = new JButton("星期一");b_week[2] = new JButton("星期二");b_week[3] = new JButton("星期三");b_week[4] = new JButton("星期四");b_week[5] = new JButton("星期五");b_week[6] = new JButton("星期六");b_week[0].setSize(400, 200);b_week[1].setSize(400, 200);b_week[2].setSize(400, 200);b_week[3].setSize(400, 200);b_week[4].setSize(400, 200);b_week[5].setSize(400, 200);b_week[6].setSize(400, 200);for (int i = 0; i < 7; i++) {b_week[i].setEnabled(false);pCenter_week.add(b_week[i]);}pCenter_day = new JPanel();for (int cols = 0; cols < 6; cols++) {for (int rows = 0; rows < 7; rows++) {b_day[cols][rows] = new JButton("");b_day[cols][rows].setSize(400, 200);this.pCenter_day.add(b_day[cols][rows]);}}pCenter_day.setLayout(new GridLayout(6, 7));setDay(Integer.parseInt(this.year.getSelectedItem().toString()),Integer.parseInt(this.month.getSelectedItem().toString()));// setDay(2011,2);pCenter.setLayout(new BorderLayout());pCenter.add(pCenter_week, "North");pCenter.add(pCenter_day, "Center");thisContainer = this.getContentPane();thisContainer.setLayout(new BorderLayout());thisContainer.add(pUp, "North");thisContainer.add(pCenter, "Center");this.setVisible(true);this.setResizable(false);this.pack();}public void setDate() {int year, month, day, week;Calendar cal = Calendar.getInstance();year = cal.get(Calendar.YEAR);month = cal.get(Calendar.MONTH);day = cal.get(Calendar.DA TE);week = cal.get(Calendar.WEEK_OF_YEAR);int year_temp = year - 4;for (int i = 0; i < 10; i++) {this.year.addItem(year_temp);year_temp += 1;}this.year.setSelectedIndex(4);for (int n = 0; n < 12; n++) {this.month.addItem(n + 1);}this.month.setSelectedIndex(month);}public void setDay(int Year, int Month) {int count;Calendar c = Calendar.getInstance();c.clear();c.set(Year, Month-1, 1);count = c.getActualMaximum(Calendar.DAY_OF_MONTH); // 总天数System.out.print(count);int day = c.get(Calendar.DAY_OF_WEEK) - 1; // 0为星期天,6为星期六System.out.print(day);int i = 1 - day;for (int cols = 0; cols < 6; cols++) {for (int rows = 0; rows < 7; rows++) {String st = String.valueOf(i);b_day[cols][rows].setText(st);b_day[cols][rows].setEnabled(false);if (i > 0 && i <= count)b_day[cols][rows].setVisible(true);elseb_day[cols][rows].setVisible(false);i++;}}}public void actionPerformed(ActionEvent e) {if (e.getSource() == b_query) {this.setDay(Integer.parseInt(this.year.getSelectedItem().toString()), Integer.parseInt(this.month.getSelectedItem().toString()));}if (e.getSource() == b_today) {int year, month;Calendar cal = Calendar.getInstance();year = cal.get(Calendar.YEAR);month = cal.get(Calendar.MONTH)+1;this.setDay(year,month);}}public static void main(String[] args) {rili rl = new rili();rl.init();}}。
Java开发日历记事本
E m i xj cc e. — a : s@cc. t n l l n c
h t :ww d z .e .n t / w.n sn t p/ o
Te h+8 55l 56 96 56 09 4 6— 一 90 3 9 6
C m ue K o  ̄d ea d T c n l y电脑 知识 与技术 o p tr n we g n e h o g o
号 码 排 列算 法 : 方 法包 含 两 个 整 形 参 数 , 该 分别 是 星期 几 和 月 天数 。该 方法 的作 用是 将 日期 一 次 排 列 人 日历 牌 的 网格 中 , 日期
2算 法描述
主 类 的算 法 :a n aP d类 首先 创 建 用 于 布 局 的 面 板 , 面 添 加 日历 牌 , 面 添加 记事 本 。然后 调 用 C ln aPd方 法 , 方 法 C l dra e 左 右 a d ra e 该
有 三 个 分 别 是 y a, n , a e r mo t d y的整 形 参 数 。该 方 法 管 理 日历 记 事 本 的 主 界 面 。在 E历 牌 部 分 使 用 网格 布 局 , h t 行数 和列 数 均 为 7 。其 中第 一 行 显示 星期 标 题 的标 签 . 续 六 行 显 示 日期 的标 签 。 后
g g .Thebai unc on fCae d rh v r w s a a em odfe . ua e s f t so l n a a e b o e nd d t c i i d The i bai unci so N o e a v np , a ,d lt ,ve . scf ton .f t p d ha e i ut sve ee e iw
java日历之GUI界面化
public void mouseClicked(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public static void main(String args[]) { Calendar calendar=Calendar.getInstance(); int y=calendar.get(Calendar.YEAR); int m=calendar.get(Calendar.MONTH)+1; int d=calendar.get(Calendar.DAY_OF_MONTH); new CalendarPad(y,m,d); } }
} for(int i=0;i<星期几;i++) { showDay[i].setText(""); } for(int i=星期几+月天数;i<42;i++) { showDay[i].setText(""); }
} if(i%7==0) { showDay[i].setForeground(Color.red); } n++;
} public void 排列号码(int 星期几,int 月天数) { for(int i=星期几,n=1;i<星期几+月天数;i++) { showDay[i].setText(""+n); if(n==day) { showDay[i].setForeground(Color.green); showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20)); } else { showDay[i].setFont(new Font("TimesRoman",Font.BOLD,12)); showDay[i].setForeground(Color.black); } if(i%7==6) { showDay[i].setForeground(Color.blue);
日历记事本
课程设计报告课程:面向对象程序设计学号:姓名:班级:计算机科学与技术2班教师:钱田芬时间: 2014年12月23日计算机学院(3)Month类,如表2-3所示:主要成员变量(属性)成员变量描述月月份显示按钮日历表变量类型intJTextFieldJButtonCalendarPad名称monthShowMonthbeforeMont、afterMonthcalendarpad表2-3:Month类表(4)NotePad类,如表2-3所示:主要成员变量(属性)成员变量描述变量类型名称文本输入区年、月、日按钮哈希表文本框日历表JTextAreaintJButtonHashtableJLabelCalendarPadtextYear、month、daySave、delete、queryhashtablelabelTextcalendarpad4.4测试与分析(1)初始化日历记事本页面如图3-1:图3-1:初始化日历记事本界面(2)向上翻年如图3-2所示:图3-2:向上翻年(3)向下翻年如图3-3所示:图3-3:向下翻年(3)向上翻月如图3-4所示:图3-4:向上翻月(4)向下翻月如图3-5所示:图3-5:向下翻月(4)保存日志如图3-6所示:图3-6:保存日志(5)删除日志如图3-7所示:图3-7:删除日志(6)查看日志如图3-8所示:图3-8:查看日志(7)查询日志如图3-9所示:图3-9:查询日志4.5程序代码(部分)CalendarPad.javaimport java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import bel;import java.awt.event.MouseEvent;。
记事本程序源代码汇总
记事本程序源代码汇总下面是一个简单的记事本程序的源代码:```#include <iostream>#include <fstream>#include <string>using namespace std;void showMencout << "**********************" << endl; cout << " 记事本程序 " << endl; cout << "**********************" << endl; cout << "请选择以下操作:" << endl;cout << "1. 新建记事本文件" << endl;cout << "2. 打开已有记事本文件" << endl; cout << "3. 查看记事本文件内容" << endl; cout << "4. 添加文本到记事本文件" << endl; cout << "5. 退出程序" << endl;cout << "**********************" << endl; void createFilstring filename;cout << "请输入新建记事本文件的文件名:";cin >> filename;//在当前目录创建一个新文件ofstream outFile(filename);outFile.close(;cout << "成功创建记事本文件:" << filename << endl; void openFilstring filename;cout << "请输入要打开的记事本文件的文件名:";cin >> filename;//尝试打开已存在的文件ifstream inFile(filename);if (!inFile)cout << "无法打开文件:" << filename << endl;} elsestring content;getline(inFile, content, '\0');cout << "记事本文件内容:" << endl;cout << content << endl;inFile.close(;}void viewFilstring filename;cout << "请输入要查看的记事本文件的文件名:"; cin >> filename;//尝试打开已存在的文件ifstream inFile(filename);if (!inFile)cout << "无法打开文件:" << filename << endl; } elsestring line;cout << "记事本文件内容:" << endl;while (getline(inFile, line))cout << line << endl;}inFile.close(;}void appendToFilstring filename;cout << "请输入要添加文本的记事本文件的文件名:";cin >> filename;//尝试打开已存在的文件ofstream outFile(filename, ios::app);if (!outFile)cout << "无法打开文件:" << filename << endl;} elsestring content;cout << "请输入要添加的文本内容(以#结束):" << endl; while (true)getline(cin, content);if (content == "#")break;}outFile << content << endl;}outFile.close(;cout << "成功添加文本到记事本文件:" << filename << endl; }int maiint choice;doshowMenu(;cout << "请输入您的选择:";cin >> choice;switch (choice)case 1:createFile(;break;case 2:openFile(;break;case 3:viewFile(;break;case 4:appendToFile(;break;case 5:cout << "感谢使用记事本程序,再见!" << endl;break;default:cout << "无效的选择,请重新输入!" << endl;}} while (choice != 5);return 0;```这个记事本程序通过命令行界面提供了以下操作:1.新建记事本文件:用户输入一个文件名后,在当前目录下创建一个新文件作为记事本。
单片机万年历程序代码
单片机万年历程序代码以下是一个示例单片机万年历的程序代码:```c#include <reg51.h>typedef unsigned char uchar;typedef unsigned int uint;sbit K1 = P2^0; // 显示年份sbit K2 = P2^1; // 显示月份sbit K3 = P2^2; // 显示日期uchar code year_tab[] = {31,28,31,30,31,30,31,31,30,31,30,31}; uchar code week_tab[] = {0x06,0x07,0x01,0x02,0x03,0x04,0x05}; uchar year, month, day, week;void delay(uint ms){uint i, j;for(i=ms;i>0;i--)for(j=110;j>0;j--);}uchar getKey(){if(K1==0) {delay(5);if(K1==0)return 1;while(!K1);}if(K2==0) {delay(5);if(K2==0)return 2;while(!K2);}if(K3==0) {delay(5);if(K3==0)return 3;while(!K3);}return 0;}void display(uchar num) {P0 = num;delay(1);P0 = 0x00;}void init(){TMOD=0x01;TH0=0xFC;TL0=0x67;EA=1;ET0=1;TR0=1;}void main(){init();while(1) {uchar key = getKey();if(key == 1) {year++;if(year == 100)year = 0;}else if(key == 2) {month++;if(month == 13)month = 1;}else if(key == 3) {day++;if(day > year_tab[month-1]) { day = 1;}}display(year / 10);display(year % 10);display(month / 10);display(month % 10);display(day / 10);display(day % 10);display(week);}}void timer0() interrupt 1{TH0=0xFC;TL0=0x67;week++;if(week == 7)week = 0;}```该代码的主要思路是通过外部三个按键模拟年、月和日的调节,通过一个定时器不断更新星期的计数,然后将年、月、日和星期分别在数码管上显示出来。
日历代码(VB实现)
日历设计VB实现最终效果如下:设计过程界面如下:Private Sub Combo1_Click()Picture1.Clsnian = Val(Combo1.Text)yue = Val(Combo2.Text)i = 0: j = 0: Sum = 0Dim y As IntegerFor y = 1900 To nian - 1If f(y) = 1 Theni = i + 1Elsej = j + 1End IfNext ySum = Sum + i * 366 + j * 365For i = 1 To yue - 1Sum = Sum + tian(nian, i)Next iDim a As Datea = #1/1/1900# + SumPicture1.Print "今天日期:"; Year(Date); "年"; Month(Date); "月"; Day(Date); "日"Picture1.PrintPicture1.Print " 日"; Tab(9); "一"; Tab(17); "二"; Tab(25); "三"; Tab(33); "四"; Tab(41); "五"; Tab(49); "六"n = Weekday(a)m = 0k = 0For i = 1 To 8 - nPicture1.Print Tab(8 * (i + n - 2)); i;Next iPicture1.PrintFor i = 9 - n To tian(nian, yue)Picture1.Print i; Tab(8 * (i + n - 8 - k));m = m + 1If m Mod 7 = 0 Then k = k + 7: Picture1.PrintNext iEnd SubPrivate Sub Combo2_Click()Picture1.Clsnian = Val(Combo1.Text)yue = Val(Combo2.Text)i = 0: j = 0: Sum = 0Dim y As IntegerFor y = 1900 To nian - 1If f(y) = 1 Theni = i + 1Elsej = j + 1Next ySum = Sum + i * 366 + j * 365For i = 1 To yue - 1Sum = Sum + tian(nian, i)Next iDim a As Datea = #1/1/1900# + SumPicture1.Print "今天日期:"; Year(Date); "年"; Month(Date); "月"; Day(Date); "日"Picture1.PrintPicture1.Print " 日"; Tab(9); "一"; Tab(17); "二"; Tab(25); "三"; Tab(33); "四"; Tab(41); "五"; Tab(49); "六"n = Weekday(a)m = 0k = 0For i = 1 To 8 - nPicture1.Print Tab(8 * (i + n - 2)); i;Next iPicture1.PrintFor i = 9 - n To tian(nian, yue)Picture1.Print i; Tab(8 * (i + n - 8 - k));m = m + 1If m Mod 7 = 0 Then k = k + 7: Picture1.PrintNext iEnd SubPrivate Sub Form_Load()ShowLabel3.Caption = "现在时间:" + Str(Time)For i = 1900 To 2100Combo1.AddItem (i)Next iCombo1.Text = Combo1.List(Year(Date) - 1900)Combo2.Text = Combo2.List(Month(Date) - 1)Picture1.Clsnian = Year(Date)yue = Month(Date)i = 0: j = 0: Sum = 0Dim y As IntegerFor y = 1900 To nian - 1If f(y) = 1 Theni = i + 1Elsej = j + 1Next ySum = Sum + i * 366 + j * 365For i = 1 To yue - 1Sum = Sum + tian(nian, i)Next iDim a As Datea = #1/1/1900# + SumPicture1.Print " 今天日期:"; nian; "年"; yue; "月"; Day(Date); "日"Picture1.PrintPicture1.Print " 日"; Tab(9); "一"; Tab(17); "二"; Tab(25); "三"; Tab(33); "四"; Tab(41); "五"; Tab(49); "六"n = Weekday(a)m = 0k = 0For i = 1 To 8 - nPicture1.Print Tab(8 * (i + n - 2)); i;Next iPicture1.PrintFor i = 9 - n To tian(nian, yue)Picture1.Print i; Tab(8 * (i + n - 8 - k));m = m + 1If m Mod 7 = 0 Then k = k + 7: Picture1.PrintNext iEnd SubFunction f(y As Integer) As IntegerIf (y Mod 4 = 0 And y Mod 100 <> 0 Or y Mod 400 = 0) Thenf = 1Elsef = 0End IfEnd FunctionFunction tian(nian, yue)Dim t As Stringt = ""Select Case yueCase 1, 3, 5, 7, 8, 10, 12tian = 31Case 4, 6, 9, 11tian = 30Case 2If (nian Mod 4 = 0 And nian Mod 100 <> 0 Or nian Mod 400 = 0) Thentian = 29Elsetian = 28End IfEnd SelectEnd FunctionPrivate Sub Label3_Click()End SubPrivate Sub Timer1_Timer()Label3.Caption = "现在时间:" + Str(Time) End Sub。
万年历代码c语言
万年历代码c语言万年历是一种实用的日历工具,它可以根据年、月、日来显示当天的日期信息,并可以切换到其他日期来查询对应的日期信息。
在编写万年历的代码时,我们需要考虑输入的年份是否为闰年、每个月的天数、以及每个月第一天是星期几等等。
以下是一份使用C语言编写万年历的参考代码:```c#include <stdio.h>// 判断是否为闰年int isLeapYear(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }// 获取某年某月的天数int getMonthDays(int year, int month) {int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return days[month - 1];}// 获取某年某月第一天是星期几int getFirstDayOfWeek(int year, int month) {int day = 1;for (int i = 1800; i < year; i++) {if (isLeapYear(i)) {day = (day + 366) % 7;} else {day = (day + 365) % 7;}}for (int i = 1; i < month; i++) {day = (day + getMonthDays(year, i)) % 7;}return day;}int main() {int year, month;printf("请输入年份:");scanf("%d", &year);printf("请输入月份:");scanf("%d", &month);// 判断输入是否合法if (year < 1800 || month < 1 || month > 12) {printf("输入的年份或月份不合法!\n");return 0;}// 获取某年某月的天数和第一天是星期几int days = getMonthDays(year, month);int firstDayOfWeek = getFirstDayOfWeek(year, month); // 打印万年历printf("日一二三四五六\n");for (int i = 0; i < firstDayOfWeek; i++) {printf(" ");}for (int i = 1; i <= days; i++) {printf("%2d ", i);if ((firstDayOfWeek + i) % 7 == 0) {printf("\n");}}printf("\n");return 0;}```以上代码实现了一个简单的万年历功能,在控制台输出给定年份和月份的月历。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
运行界面:// CalendarWindow类package others;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;import java.awt.SplashScreen;public class CalendarWindow extends JFrame implements ActionListener,MouseListener,FocusListener {Calendar calendar;int year,month,day;CalendarMessage calendarMessage;CalendarPad calendarPad;NotePad notePad;JTextField showYear,showMonth;JTextField showDay[];CalendarImage calendarImage;String picturename;//图像名称;String getPicture_address;Alarm alarm;JButton nextYear,previousYear,nextMonth,previousMonth;JButton saveDailyRecord,deleteDailyRecord,readDailyRecord,changeBackground;File dir;Color backColor=Color.white;public CalendarWindow(){dir= new File("./dailyRecord");dir.mkdir();showDay = new JTextField[42];for(int i=0;i<showDay.length;i++){showDay[i]=new JTextField();showDay[i].setBackground(backColor);showDay[i].setLayout(new GridLayout(3,3));}calendarMessage = new CalendarMessage();calendarPad = new CalendarPad();notePad = new NotePad();Calendar calendar = Calendar.getInstance();calendar.setTime(new Date());year = calendar.get(Calendar.YEAR);month = calendar.get(Calendar.MONTH)+1;day = calendar.get(Calendar.DAY_OF_MONTH);calendarMessage.setYear(year);calendarMessage.setMonth(month);calendarMessage.setDay(day);calendarPad.setCalendarMessage(calendarMessage);calendarPad.setShowDayTextField(showDay);notePad.setMessage(year,month,day);calendarPad.showMonthCalendar();doMark();calendarImage = new CalendarImage();picturename=getPicture_address();//calendarImage.setImageFile(new File(picturename));alarm=new Alarm();JSplitPane splitV1 = newJSplitPane(JSplitPane.VERTICAL_SPLIT,calendarPad,calendarImage);JSplitPane splitV2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,notePad,alarm); JSplitPane splitH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitV1,splitV2); add(splitH,BorderLayout.CENTER);showYear = new JTextField(""+year,6);showYear.setFont(new Font("TimesRoman",Font.BOLD,12));showYear.setHorizontalAlignment(JTextField.CENTER);showMonth = new JTextField(""+month,4);showMonth.setFont(new Font("TimesRoman",Font.BOLD,12));showMonth.setHorizontalAlignment(JTextField.CENTER);nextYear = new JButton("下年");previousYear = new JButton("上年");nextMonth = new JButton("下月");previousMonth = new JButton("上月");nextYear.addActionListener(this);previousYear.addActionListener(this);nextMonth.addActionListener(this);previousMonth.addActionListener(this);showYear.addActionListener(this);showMonth.addActionListener(this);JPanel north = new JPanel();north.add(previousYear);north.add(showYear);north.add(nextYear);north.add(previousMonth);north.add(showMonth);north.add(nextMonth);add(north,BorderLayout.NORTH);changeBackground = new JButton("更改背景图片");saveDailyRecord = new JButton("保存日志");deleteDailyRecord = new JButton("删除日志");readDailyRecord = new JButton("读取日志");changeBackground.addActionListener(this);saveDailyRecord.addActionListener(this);JPanel pSouth = new JPanel();pSouth.add(changeBackground);pSouth.add(saveDailyRecord);pSouth.add(deleteDailyRecord);pSouth.add(readDailyRecord);add(pSouth,BorderLayout.SOUTH);setVisible(true);setBounds(70,70,770,500);validate();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public String getPicture_address(){String address=null;try{FileInputStream outOne=new FileInputStream("picture_address.txt");ObjectInputStream outTwo=new ObjectInputStream(outOne);try{address=(String)outTwo.readObject();}catch(Exception ex){}outTwo.close();}catch(IOException eee){}if(address!=null){return address;}else{return"picture.jpg";}}public void actionPerformed(ActionEvent e){if(e.getSource()==changeBackground){FileDialog dia=new FileDialog(this,"选定图片",FileDialog.LOAD);//新建一个文件选择对话框,用dia获得选择得到的信息;dia.setModal(true);//设置为模态对话框;dia.setVisible(true);if((dia.getDirectory()!=null)&&(dia.getFile()!=null))//如果得到的路径和文件名不为空;{try{FileOutputStream inOne=new FileOutputStream("picture_address.txt");ObjectOutputStream inTwo=new ObjectOutputStream(inOne);inTwo.writeObject(dia.getDirectory()+dia.getFile());inTwo.close();}catch(IOException ee){}picturename=getPicture_address();//从文件中读出显示图片的路径地址;calendarImage.setImageFile(new File(picturename));//将获得路径传给图片,显示该路径指定}}if(e.getSource()==nextYear){year++;showYear.setText(""+year);calendarMessage.setYear(year);calendarPad.setCalendarMessage(calendarMessage);calendarPad.setShowDayTextField(showDay);calendarPad.showMonthCalendar();notePad.setMessage(year,month,day);doMark();}else if (e.getSource()==previousYear){year--;showYear.setText(""+year);calendarMessage.setYear(year);calendarPad.setCalendarMessage(calendarMessage);calendarPad.setShowDayTextField(showDay);calendarPad.showMonthCalendar();notePad.setMessage(year,month,day);doMark();}else if (e.getSource()==nextMonth){month++;if(month>12){month=1;year++;}showYear.setText(""+year);showMonth.setText(""+month);calendarMessage.setMonth(month);calendarPad.setCalendarMessage(calendarMessage);calendarPad.setShowDayTextField(showDay);calendarPad.showMonthCalendar();notePad.setMessage(year,month,day);doMark();}else if (e.getSource()==previousMonth){month--;if(month<1){month=12;year--;}showYear.setText(""+year);showMonth.setText(""+month);calendarMessage.setMonth(month);calendarPad.setCalendarMessage(calendarMessage);calendarPad.setShowDayTextField(showDay);calendarPad.showMonthCalendar();notePad.setMessage(year,month,day);doMark();else if (e.getSource()==showYear){String s = showYear.getText().trim();char a[] = s.toCharArray();boolean boo = false;for(int i = 0;i < a.length;i++){if(!(Character.isDigit(a[i])))boo = true;}if(boo==true)JOptionPane.showMessageDialog(this,"你输入了非法年份","警告!",JOptionPane.WARNING_MESSAGE);else if(boo==false)year = Integer.parseInt(s);showYear.setText(""+year);calendarMessage.setYear(year);calendarPad.setCalendarMessage(calendarMessage);calendarPad.showMonthCalendar();notePad.setMessage(year,month,day);doMark();}else if(e.getSource()==showMonth){String s = showMonth.getText().trim();char a[] = s.toCharArray();boolean boo = false;for(int i = 0;i < a.length;i++){if(!(Character.isDigit(a[i])))boo = true;}if(boo==true)JOptionPane.showMessageDialog(this,"你输入了非法月份","警告!",JOptionPane.WARNING_MESSAGE);else if(boo==false)month = Integer.parseInt(s);showMonth.setText(""+month);calendarMessage.setYear(month);calendarPad.setCalendarMessage(calendarMessage);calendarPad.showMonthCalendar();notePad.setMessage(year,month,day);doMark();}else if (e.getSource()== saveDailyRecord){notePad.savefile(dir,year,month,day);doMark();}else if(e.getSource()==deleteDailyRecord){notePad.deletefile(dir,year,month,day);doMark();}else if (e.getSource()==readDailyRecord){doMark();}}public void mousePressed(MouseEvent e){JTextField text = (JTextField)e.getSource();String str = text.getText().trim();try{day = Integer.parseInt(str);}catch (NumberFormatException exp){}calendarMessage.setDay(day);notePad.setMessage(year,month,day);}public void mouseReleased(MouseEvent e){}public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){}public void mouseClicked(MouseEvent e){}public void focusGained(FocusEvent e){Component com = (Component)e.getSource();com.setBackground(Color.blue);}public void focusLost(FocusEvent e){Component com = (Component)e.getSource();com.setBackground(backColor);}public void doMark(){for(int i=0;i<showDay.length;i++){showDay[i].removeAll();String str = showDay[i].getText().trim();try{int n = Integer.parseInt(str);if(isHaveDailyRecord(n)==true){JLabel mess = new JLabel("yes");mess.setFont(new Font("TimesRoman",Font.PLAIN,11));mess.setForeground(Color.red);showDay[i].add(mess);showDay[i].setBackground(Color.pink);}if(n==calendar.get(Calendar.DAY_OF_MONTH)){showDay[i].setFont(new Font("微软雅黑",Font.ITALIC,23));//showDay[i].setBackground(Color.DARK_GRAY);showDay[i].setForeground(Color.red);}}catch (Exception exp){}calendarPad.repaint();calendarPad.validate();}public boolean isHaveDailyRecord(int n){String key = ""+year+""+month+""+n;String []dayFile = dir.list();boolean boo = false;for(int k = 0;k<dayFile.length;k++){if(dayFile[k].equals(key+".txt")){boo = true;break;}}return boo;}public static void main(String args[]){new CalendarWindow();}}//calendarPad类package others;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class CalendarPad extends JPanel{int year,month,day;CalendarMessage calendarMessage;JTextField []showDay;JLabel title [];String [] week={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};JPanel north,center;public CalendarPad(){setLayout(new BorderLayout());north=new JPanel();north.setLayout(new GridLayout(1,7));center=new JPanel();center.setLayout(new GridLayout(6,7));add(center,BorderLayout.CENTER);add(north,BorderLayout.NORTH);title=new JLabel[7];for(int j=0;j<7;j++){title[j]=new JLabel();title[j].setFont(new Font("TimesRoman",Font.BOLD,12));title[j].setText(week[j]);title[j].setHorizontalAlignment(JLabel.CENTER);title[j].setBorder(BorderFactory.createRaisedBevelBorder());north.add(title[j]);}title[0].setForeground(Color.red);title[6].setForeground(Color.blue);}showDay=text;for(int i=0;i<showDay.length;i++){showDay[i].setFont(new Font("TimesRoman",Font.BOLD,15));showDay[i].setBackground(Color.WHITE);showDay[i].setHorizontalAlignment(JTextField.CENTER);showDay[i].setEditable(false);center.add(showDay[i]);}}public void setCalendarMessage(CalendarMessage calendarMessage){this.calendarMessage=calendarMessage;}public void showMonthCalendar(){String [] a=calendarMessage.getMonthCalendar();for(int i=0;i<42;i++){showDay[i].setText(a[i]);}validate();}}//NotePad类package others;import java.awt.*;import javax.swing.*;import java.io.*;import java.awt.event.*;class NotePad extends JPanel implements ActionListener,ItemListener {JTextField ShowMessage;JTextArea text;JLabel l1;JPanel p1,p2,p3;Choice fontlist,sizelist;JButton changecolor;String Size[]={"10","12","14","16","18","20","22","24","26","28","30"};NotePad(){ShowMessage=new JTextField();ShowMessage.setEditable(false);text=new JTextArea(10,20);p1=new JPanel();p2=new JPanel();p3=new JPanel();l1=new JLabel("记事本字体");changecolor=new JButton("颜色");fontlist=new Choice();sizelist=new Choice();GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();String fontname[]=ge.getAvailableFontFamilyNames();for(int i=0;i<fontname.length;i++){fontlist.add(fontname[i]);}for(int i=0;i<Size.length;i++){}p1.add(l1);p1.add(fontlist);p1.add(sizelist);p1.add(changecolor);setLayout(new BorderLayout());add(ShowMessage,BorderLayout.NORTH);p2.setLayout(new BorderLayout());p2.add(new JScrollPane(text),BorderLayout.CENTER);p2.add(p3,BorderLayout.SOUTH);add(p2,BorderLayout.CENTER);add(p1,BorderLayout.SOUTH);setBounds(0,0,400,400);setVisible(true);changecolor.addActionListener(this);fontlist.addItemListener(this);sizelist.addItemListener(this);}public void setMessage(int year,int month,int day){ShowMessage.setText(year+"年"+month+"月"+day+"日");ShowMessage.setHorizontalAlignment(JLabel.CENTER);ShowMessage.setForeground(Color.blue);ShowMessage.setFont(new Font("宋体",Font.BOLD,15));text.setText("");}public void actionPerformed(ActionEvent e){if(e.getSource()==changecolor){Color newColor=JColorChooser.showDialog(this, "选择颜色", text.getForeground());if(newColor!=null){text.setForeground(newColor);}}}public void itemStateChanged(ItemEvent e){String n1=sizelist.getSelectedItem();String name=fontlist.getSelectedItem();int n2=Integer.parseInt(n1);Font f=new Font(name,Font.PLAIN,n2);text.setFont(f);}public void savefile(File dir,int year,int month,int day){String dailyRecord=text.getText();String fileName=""+year+""+month+""+day+".txt";String key=""+year+""+month+""+day;String dialyFile[]=dir.list();boolean b=false;for(int i=0;i<dialyFile.length;i++){if(dialyFile[i].startsWith(key)){}}if(b){int n=JOptionPane.showConfirmDialog(this,""+year+"年"+month+"月"+day+"日"+"已经有日志存在,是否添加日志?","确认对话框",JOptionPane.YES_NO_OPTION);if(n==JOptionPane.YES_OPTION){try{File file=new File(dir,fileName);RandomAccessFile out=new RandomAccessFile(file,"rw");long end=out.length();byte[]bb=dailyRecord.getBytes();out.seek(end);out.write(bb);out.close();}catch(IOException e){}text.setText("");}else{text.setText("");}}else{int n=JOptionPane.showConfirmDialog(this,"是否添加"+year+"年"+month+"月"+day+"日"+"日志?","确认对话框",JOptionPane.YES_NO_OPTION);if(n==JOptionPane.YES_OPTION){try{File file=new File(dir,fileName);FileWriter fw=new FileWriter(file);BufferedWriter bw=new BufferedWriter(fw);bw.write(dailyRecord);bw.close();fw.close();}catch(IOException e){}JOptionPane.showMessageDialog(this,"添加日志成功","消息对话框",RMATION_MESSAGE);text.setText("");}}}public void deletefile(File dir,int year,int month,int day){String key=""+year+""+month+""+day;String dialyFile[]=dir.list();boolean b=false;for(int i=0;i<dialyFile.length;i++){if(dialyFile[i].startsWith(key)){break;}}if(b){int n=JOptionPane.showConfirmDialog(this,"是否删除"+year+"年"+month+"月"+day+"日的日志?","确认对话框",JOptionPane.YES_NO_OPTION);if(n==JOptionPane.YES_OPTION){try{String fileName=""+year+""+month+""+day+".txt";File file=new File(dir,fileName);file.delete();}catch(Exception e){}text.setText("");}}else{JOptionPane.showMessageDialog(this,""+year+"年"+month+"月"+day+"日无日志!","消息对话框",RMATION_MESSAGE);}}public void readfile(File dir,int year,int month,int day){String fileName=""+year+""+month+""+day+".txt";String key=""+year+""+month+""+day;String dialyFile[]=dir.list();boolean b=false;for(int i=0;i<dialyFile.length;i++){if(dialyFile[i].startsWith(key)){b=true;break;}}if(b){text.setText("");try{File file=new File(dir,fileName);FileReader inOne=new FileReader(file);BufferedReader inTwo=new BufferedReader(inOne);String s;while((s=inTwo.readLine())!=null){text.append(s+"\n");}inOne.close();inTwo.close();}catch(IOException e){}}else{JOptionPane.showMessageDialog(this,""+year+"年"+month+"月"+day+"日无日志!","消息对话框",RMATION_MESSAGE);}}}//CalendarMessage类package others;import java.util.Calendar;public class CalendarMessage{int year=-1,month=-1,day=-1;public void setYear(int year){this.year=year;}public int getYear(){return year;}public void setMonth(int month){if(month<=12&&month>=1)this.month=month;elsethis.month=1;}public int getMonth(){return month;}public void setDay(int day){this.day=day;}public int getday(){return day;}public String [] getMonthCalendar(){String [] day=new String[42];Calendar rili=Calendar.getInstance();rili.set(year,month-1,1);//将日历翻到year年month月1日int星期几=rili.get(Calendar.DAY_OF_WEEK)-1;int dayAmount=0;if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) dayAmount=31;if(month==4||month==6||month==9||month==11)dayAmount=30;if(month==2){if(((year%4==0)&&(year%100!=0))||year%400==0)dayAmount=29;elsedayAmount=28;}for(int i=0;i<星期几;i++)day[i]="";for(int i=星期几,n=1;i<星期几+dayAmount;i++){day[i]=String.valueOf(n);n++;}for (int i=星期几+dayAmount;i<42;i++ )day[i]="";return day;}}// CalendarImage类package others;import javax.swing.*;import java.io.*;import java.awt.*;public class CalendarImage extends JPanel{File imageFile;Image image;Toolkit tool;//构造方法public CalendarImage() {tool=getToolkit();//获取工具;}//设置图像文件;public void setImageFile(File f){imageFile =f;try{image=tool.getImage(imageFile.toURI().toURL());//得到图像文件;}catch (Exception eeee){}repaint();//重绘;}//重载paintComponent方法,覆盖原来的,供repaint()调用;public void paintComponent(Graphics g){super.paintComponent(g);int w=getBounds().width;int h=getBounds().height;g.drawImage(image,0,0,w,h,this);}}//Alarm类package others;import java.applet.Applet;import java.applet.AudioClip;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.io.File;import .MalformedURLException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import javax.swing.*;public class Alarm extends JPanel implements Runnable {JLabel ri ,shi, fen, miao, dangqian;JButton queding, dakai;JTextField music,day, hour, minute, second;int h=0,f=0,m=0,r=0;boolean fo=false;public AudioClip soumd1;public Alarm() {setLayout(new GridLayout(3, 1));JPanel jp = new JPanel();dangqian = new JLabel();jp.add(dangqian);add(jp);JPanel jp1 = new JPanel();music = new JTextField(20);dakai = new JButton("选择闹铃音乐");jp1.add(music);jp1.add(dakai);add(jp1);ri = new JLabel("日");day = new JTextField(4);shi = new JLabel("时");hour = new JTextField(4);fen = new JLabel("分");minute = new JTextField(4);miao = new JLabel("秒");second = new JTextField(4);JPanel jp2 = new JPanel();jp2.add(ri);jp2.add(day);jp2.add(shi);jp2.add(hour);jp2.add(fen);jp2.add(minute);jp2.add(miao);jp2.add(second);queding = new JButton("确定");jp2.add(queding);add(jp2);setSize(400, 130);setVisible(true);dakai.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) {JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可fileChooser.setCurrentDirectory(new File(".")); // 设置文件选择器当前目录fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {public boolean accept(File file) { // 可接受的文件类型String name = file.getName().toLowerCase();return name.endsWith(".wav")|| name.endsWith(".au")|| file.isDirectory();}public String getDescription() { // 文件描述return "音乐文件(*.wav,*.au)";}});if (fileChooser.showOpenDialog(Alarm.this) == JFileChooser.APPROVE_OPTION) { // 弹出文件选择器,并判断是否点击了打开按钮String fileName = fileChooser.getSelectedFile().getAbsolutePath(); // 得到选择文件或目录的绝对路径music.setText(fileName);}}});queding.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) {if(queding.getText().equals("确定")){try{r=Integer.parseInt(day.getText());h=Integer.parseInt(hour.getText());f=Integer.parseInt(minute.getText());m=Integer.parseInt(second.getText());if(1<=h&&h<=31&&0<=h&&h<=23&&0<=f&&f<=59&&0<=m&&m<=59){fo=true;}elseJOptionPane.showMessageDialog(null, "输入时间错误");}catch(Exception e){JOptionPane.showMessageDialog(null, "请输入正确的时间");}}else{fo=false;day.setEditable(true);hour.setEditable(true);minute.setEditable(true);second.setEditable(true);queding.setText("确定");//soumd1.stop();}}});Thread t1 = new Thread(this);t1.start();}public void run() {while (true) {Date now = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒",Locale.SIMPLIFIED_CHINESE);dangqian.setFont(new Font("微软雅黑",Font.BOLD,20));dangqian.setForeground(Color.RED);dangqian.setText("当前时间: " + sdf.format(now));SimpleDateFormat ri=new SimpleDateFormat("dd"); //封装为了获取日期SimpleDateFormat shi=new SimpleDateFormat("kk"); //封装为了获取小时SimpleDateFormat fen=new SimpleDateFormat("mm"); //封装为了获取分钟SimpleDateFormat miao=new SimpleDateFormat("ss"); //封装为了获取秒钟int riqi=Integer.parseInt(ri.format(now)); //获取日期int shizhong=Integer.parseInt(shi.format(now)); //获取小时int fenzhong=Integer.parseInt(fen.format(now)); //获取分钟int miaozhong=Integer.parseInt(miao.format(now)); //获取秒钟if(fenzhong==0&&miaozhong==0)try {soumd1=Applet.newAudioClip(new File(music.getText()).toURL()); //播放音乐soumd1.play();fo=false;} catch (MalformedURLException e) {e.printStackTrace();}if(fo){day.setEditable(false);hour.setEditable(false);minute.setEditable(false);second.setEditable(false);queding.setText("关闭");if(riqi==r&&shizhong==h&&fenzhong==f&&miaozhong==m) //判断条件{try {soumd1=Applet.newAudioClip(new File(music.getText()).toURL()); //播放音乐soumd1.play();fo=false;} catch (MalformedURLException e) {e.printStackTrace();}}}try {Thread.sleep(1000);} catch (InterruptedException ie) {}}}}。