站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题用java编写一个万年历

用java编写一个万年历

摘要: 用java编写一个万年历


这是一个作业,我是初学者。编写过程中,关于闰年判断还好办,可是判断星期几就难了,我在百度知道上找到一个什么蔡勒公式,完成后每年的1.2.10.11.12都能正常显示,其他都有错误。程序如下,有兴趣和能力的大侠给看看
import javax.swing.*;
class work2
{
public static void main(String args[])
{
int y,m;
String year,month;
year=JOptionPane.showInputDialog( -#34;请输入年份:-#34; );
month=JOptionPane.showInputDialog(-#34;请输入月份:-#34;);
y = Integer.parseInt( year );
m=Integer.parseInt(month);
int mon[]={29,31,28,31,30,31,30,31,31,30,31,30,31};

if (y%100==0-#38;-#38;y%400==0||y%100!=0-#38;-#38;y%4==0)
{
mon[2]=mon[0];
}
int mm=0;
if(m==1||m==2)
{
y=y-1;
mm=m+12;
}
else mm=m;
int yy,c;
int d=1,x=0,ye,w;
c=(int)(y/100.0);
ye=y;
while(ye-#62;=100)
{
ye=ye-100;
x=ye;
}
w=x+(x/4)+(c/4)-2*c+(26*(mm+1)/10)+d-1;
if(w-#60;=0)
{
w=w*(-1);
}
int xq=w%7;

JTextArea outputArea = new JTextArea();
String output=-#34; sun mon tue wed thu fri sta/n-#34;+xq;
for(int tt=(-1)*xq,ttt=0;tt-#60;= mon[m];tt++,ttt++)
{
if(tt-#60;0)
{
output+=-#34; -#34;;

}
if(tt==0){}
if(tt-#62;=1-#38;tt-#60;10)
{
output+=-#34; -#34;+tt;
if(ttt%7==0)
{
output+=-#34;/n-#34;;
}

}
if(tt-#62;=10)
{
output+=-#34; -#34;+tt;
if(ttt%7==0)
{
output+=-#34;/n-#34;;
}

}

}
outputArea.setText(output);
JOptionPane.showMessageDialog( null, outputArea,-#34;万年历-#34;,JOptionPane.INFORMATION_MESSAGE );
System.exit(0);




}


}


你不用 蔡勒公式 判断每一天的是星期几
你只要算出,需要的打出的年份第一天是星期几就可以了

下边这个是网上的一个例子
提示:公元元年的第一天是星期日
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
这个是个万年历程序

import java.io.*;
class putout{
public void putout(int f,int x,int y){
int i;
int a[]= new int[40];
System.out.println(-#34; 日 一 二 三 四 五 六 -#34;+-#34; -#34;+f+-#34;月-#34;);
for (i=0;i-#60;x;i++)
{System.out.print(-#34; -#34;); }
for(i=x;i-#60;x+y;i++)
a[i]=i-x+1;
for(i=x;i-#60;x+y;i++)
{
if ((i%7==0)-#38;-#38;(i-#62;0))
System.out.print(-#34;/n-#34;);
if (a[i]-#60;10)
System.out.print(-#34; -#34;+a[i]);
else System.out.print(-#34; -#34;+a[i]);
}
System.out.println(-#34;/n-#34;);
}
}

class st{
public static void main(String args[])throws IOException{
putout p=new putout();
int year,mouth,y=1,t,i;
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.print(-#34;请输入一个年份:-#34;);
String s=in.readLine();
year=Integer.parseInt(s);
if((year%4==0 -#38;-#38; year%100!=0)||(year%400==0))
mouth=1;
else
mouth=0;
y=year;
for(i=1;i-#60;year;i++)
{if((i%4==0 -#38;-#38; i%100!=0)||(i%400==0))
y++;}
y=y%7;
for(i=1;i-#60;13;i++){
switch(i){
case 1: {p.putout(1,y,31);y=(y+31)%7;break;}
case 2: {p.putout(2,y,28+mouth);y=(y+28+mouth)%7;break;}
case 3: {p.putout(3,y,31);y=(y+31)%7;break;}
case 4: {p.putout(4,y,30);y=(y+30)%7;break;}
case 5: {p.putout(5,y,31);y=(y+31)%7;break;}
case 6: {p.putout(6,y,30);y=(y+30)%7;break;}
case 7: {p.putout(7,y,31);y=(y+31)%7;break;}
case 8: {p.putout(8,y,31);y=(y+31)%7;break;}
case 9: {p.putout(9,y,30);y=(y+30)%7;break;}
case 10: {p.putout(10,y,31);y=(y+31)%7;break;}
case 11: {p.putout(11,y,30);y=(y+30)%7;break;}
case 12: {p.putout(12,y,31);y=(y+31)%7;break;}
}
}
}
}


import java.io.*;
class putout{
public void putout(int f,int x,int y){
int i;
int a[]= new int[40];
System.out.println(-#34; 日 一 二 三 四 五 六 -#34;+-#34; -#34;+f+-#34;月-#34;);
for (i=0;i-#60;x;i++)
{System.out.print(-#34; -#34;); }
for(i=x;i-#60;x+y;i++)
a[i]=i-x+1;
for(i=x;i-#60;x+y;i++)
{
if ((i%7==0)-#38;-#38;(i-#62;0))
System.out.print(-#34;/n-#34;);
if (a[i]-#60;10)
System.out.print(-#34; -#34;+a[i]);
else System.out.print(-#34; -#34;+a[i]);
}
System.out.println(-#34;/n-#34;);
}
}

class st{
public static void main(String args[])throws IOException{
putout p=new putout();
int year,mouth,y=1,t,i;
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.print(-#34;请输入一个年份:-#34;);
String s=in.readLine();
year=Integer.parseInt(s);
if((year%4==0 -#38;-#38; year%100!=0)||(year%400==0))
mouth=1;
else
mouth=0;
y=year;
for(i=1;i-#60;year;i++)
{if((i%4==0 -#38;-#38; i%100!=0)||(i%400==0))
y++;}
y=y%7;
for(i=1;i-#60;13;i++){
switch(i){
case 1: {p.putout(1,y,31);y=(y+31)%7;break;}
case 2: {p.putout(2,y,28+mouth);y=(y+28+mouth)%7;break;}
case 3: {p.putout(3,y,31);y=(y+31)%7;break;}
case 4: {p.putout(4,y,30);y=(y+30)%7;break;}
case 5: {p.putout(5,y,31);y=(y+31)%7;break;}
case 6: {p.putout(6,y,30);y=(y+30)%7;break;}
case 7: {p.putout(7,y,31);y=(y+31)%7;break;}
case 8: {p.putout(8,y,31);y=(y+31)%7;break;}
case 9: {p.putout(9,y,30);y=(y+30)%7;break;}
case 10: {p.putout(10,y,31);y=(y+31)%7;break;}
case 11: {p.putout(11,y,30);y=(y+30)%7;break;}
case 12: {p.putout(12,y,31);y=(y+31)%7;break;}
}
}
}
}


首先谢谢给我提供的程序。我在程序中是用蔡勒公式算每个月的第一天是星期几。其中例如计算2006年4月时,用w=x+[x/4]+[c/4]-2*c+(26*(m+1)/10)+d-1计算。x为6是2006年,c为20是21世纪减去1,m为4是4月,d为1是4月1号。按理说w除以7的余树应该是6,可是我笔算和程序中都得出的是1。我很费解。但是1.2.10.11.12月都没有问题。请问各位高人这是为什么,我哪里错了。谢谢


学习


不知道你算得是哪一年,蔡勒公式,好像只是针对1582年以后的年份有效

好吧,我算了一下,发现结果等于2006.4.1的w是-15,对7求余的话是-1
就是说,是星期日的前一天,那就是星期六了
你直接就绝对值 取正了吧,我也没有想到,居然还符号

这篇文章就是星期几的事情,还有推导过程,不过你只需要用当年第一天是星期几就好了

如何计算某一天是星期几?
http://www.blog.edu.cn/user1/8722/archives/2005/332784.shtml


package MonthTest;
import java.util.*;

/**
*打印日历
*/
class MonthDemo{
Calendar today;
int month;
int year;
int weekday;
int day;
MonthDemo(){
today=Calendar.getInstance();
}
void shezhi(int y,int m){
today.clear();
today.set(Calendar.YEAR,y);
today.set(Calendar.MONTH,m);
today.set(Calendar.DAY_OF_MONTH, 1);
month=today.get(Calendar.MONTH)+1;
year=today.get(Calendar.YEAR);
weekday=today.get(Calendar.DAY_OF_WEEK);
}
void xianshi(){
System.out.println(-#34;/t/t/t -#34;+year+-#34;年-#34;+month+-#34;月-#34;);
System.out.println(-#34;/t日/t一/t二/t三/t四/t五/t六-#34;);
for(int i=0;i-#60;weekday;i++){
System.out.print(-#34;/t-#34;);
}
do{
System.out.print(today.get(Calendar.DATE)+-#34;/t-#34;);
if(today.get(Calendar.DAY_OF_WEEK)==7)
System.out.print(-#34;/n/t-#34;);
today.add(Calendar.DAY_OF_MONTH,1);
}while(today.get(Calendar.MONTH)==(month-1));
}
}
public class MonthTest{
public static void main(String args[]){
int y,m;
try{
MonthDemo Md=new MonthDemo();
y=Integer.parseInt(args[0]);
m=Integer.parseInt(args[1]);
Md.shezhi(y,m);
Md.xianshi();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
就用util包中的Calendar对象就能实现


上星期有一个一样的帖子


其实楼主只要算公元1年1月1日到今天相差的天数然后再%7
公元1年1月1日是周一好像


楼主要是还不会 我可以帮忙写个代码


元年元月元日就是星期一,不知道Java里有没有函数可以直接计算从某一天到某一天的间隔天数


我这里有现成的


路过

深圳java程序员博客,为你提供多方面资料http://drivemewild.blogchina.com


弱弱的问下你们,为什么不用util包中的Calendar呢?
我感觉比那样写算法来的简单。。


十分感谢DrinkT,我的万年历已经好用了,问题就是你说的,如果是负数要加7。
也谢谢其他人,我是初学者,对于大家说的util包的使用,我还学过。今后可能还要问大家,希望各位高人到时不要吝啬。
再次谢谢每位回帖的人!


↑返回目录
前一篇: 问个简单的,如何得到long的长度?
后一篇: 请高手看一下这段代码