用JAVA做个简单的程序。

设计题目:编制一个日期计算程序。设计要求:1.由用户输入一个月份,屏幕上输出这个月的月历,每星期一行,从星期日开始,到星期六结束。2.由用户输入一个日期,屏幕上回答是星期... 设计题目:编制一个日期计算程序。
设计要求:
1.由用户输入一个月份,屏幕上输出这个月的月历,每星期一行,从星期日开始,到星期六结束。
2.由用户输入一个日期,屏幕上回答是星期几。
3.由用户输入两个日期,计算这两个日期之间相差多少天。
展开
 我来答
pingia
2010-06-02 · TA获得超过568个赞
知道小有建树答主
回答量:697
采纳率:100%
帮助的人:765万
展开全部
你看看完全符合你的要求 也做了注释

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

/**
*
*/
/**
* @author think
*
*/
public class Test1
{
/**
*
*/
public Test1()
{
// TODO Auto-generated constructor stub
}

/**
* function:
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
new Test1().print();
}

/**
* function: 打印函数 包含对当前输入的月历的打印 以及输入和输出的打印
*/
public void print(){

//当月天数
//输入你的查询年月 如果直接输入月 默认是当前年

Scanner scanner = new Scanner(System.in);

System.out.println("1.please input the month:");
int month = scanner.nextInt();

while(month <= 0 || month >12)
{
System.out.println("1.please input the month:");
month = scanner.nextInt();
}

int days = days(month);

String[] weeks = {"sun","mon","tue","wed","thr","Frd","sat"};

for (int i = 0; i < weeks.length; i++)
{
System.out.print(weeks[i]);

for (int j = 0; j < 2; j++)
{
System.out.print(" ");
}

}
System.out.println();

int weekOfday = getWeekOfday(month,1); //获得第一天的星期

for (int i = 0; i < (weekOfday - 1) * 5; i++)
{
System.out.print(" ");
}

for (int i = 1; i <= days; i++,weekOfday +=1)
{
if(weekOfday == 8)
{
weekOfday = 1;

System.out.println();
System.out.print(i);

}
else
{
System.out.print(i);
}

for (int j = 0; j < 5 - (String.valueOf(i).length()); j++)
{
System.out.print(" ");
}

}

System.out.println("\n");

System.out.println("2.please input the date(format is like:20030204):");
String date = scanner.next();

while(date.length() < 8)
{
System.out.println("2.please input the date(format is like:20030204):");
date = scanner.next();
}

System.out.println("your input date weekday is:" + (getWeekDayStr(getWeekOfday(date))));

System.out.println("\n");

System.out.println("3.please input the compute date1(format is like:20030204):");
String date1 = scanner.next();

while(date.length() < 8)
{
System.out.println("3.please input the compute date1(format is like:20030204):");
date1 = scanner.next();
}

System.out.println("3.please input the compute date2(format is like:20030204):");
String date2 = scanner.next();

while(date.length() < 8)
{
System.out.println("3.please input the compute date2(format is like:20030204):");
date2 = scanner.next();
}

int xiangchadays = daysxiangcha(date1, date2);

System.out.println("xiangcha days is:" + xiangchadays);
}

/**
* function: 通过准确的年月日查询星期几的整数表示
* @param year
* @param month
* @param day
* @return
*/
public int getWeekOfday(int year,int month, int day)
{
Calendar c = Calendar.getInstance();

c.set(Calendar.YEAR, year);

c.set(Calendar.MONTH, month - 1);

c.set(Calendar.DAY_OF_MONTH, day);

return c.get(Calendar.DAY_OF_WEEK);
}

/**
* function: 通过输入月日 查询星期几的整数表示 默认是当前年
* @param month
* @param day
* @return
*/
public int getWeekOfday(int month, int day)
{
Calendar c = Calendar.getInstance();

c.setTime(new Date());

int year = c.get(Calendar.YEAR);

return getWeekOfday(year, month, day);
}

/**
* function: 通过日期字符串获得星期几的整数表示
* @param dateStr
* @return
*/
public int getWeekOfday(String dateStr)
{

int year =Integer.parseInt(dateStr.substring(0, 4)) ;
int month =Integer.parseInt(dateStr.substring(4, 6)) ;
int day =Integer.parseInt(dateStr.substring(6, 8)) ;

return getWeekOfday(year, month, day);
}

/**
* function: 返回星期几这样的表示
* @param weekday
* @return
*/
public String getWeekDayStr(int weekday)
{

return weekDayStrs[weekday - Calendar.SUNDAY];
}

String[] weekDayStrs = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

/**
* function: 两个日期的相差天数
* @param dateStr1 20100205这样的格式
* @param dateStr2 20100205这样的格式
* @return
*/
public int daysxiangcha(String dateStr1,String dateStr2)
{
int year1 =Integer.parseInt(dateStr1.substring(0, 4)) ;
int month1 =Integer.parseInt(dateStr1.substring(4, 6)) ;
int day1 =Integer.parseInt(dateStr1.substring(6, 8)) ;

int year2 =Integer.parseInt(dateStr2.substring(0, 4)) ;
int month2 =Integer.parseInt(dateStr2.substring(4, 6)) ;
int day2 =Integer.parseInt(dateStr2.substring(6, 8)) ;

Calendar c1 = Calendar.getInstance();

c1.set(Calendar.YEAR, year1);

c1.set(Calendar.MONTH, month1 -1);

c1.set(Calendar.DAY_OF_MONTH, day1);

Calendar c2 = Calendar.getInstance();

c2.set(Calendar.YEAR, year2);

c2.set(Calendar.MONTH, month2 - 1);

c2.set(Calendar.DAY_OF_MONTH, day2);

long mills = c1.getTimeInMillis() > c2.getTimeInMillis()?
c1.getTimeInMillis() - c2.getTimeInMillis():
c2.getTimeInMillis() - c1.getTimeInMillis();

return (int)(mills/1000/3600/24);
}

/**
* @param year
* @param month
* @return 某年某月的天数
*/
public static int days(int year,int month)
{
int days = 0;
if(month!=2)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days = 31 ;break;
case 4:
case 6:
case 9:
case 11:days = 30;

}
}
else
{

if(year%4==0 && year%100!=0 || year%400==0)
days = 29;
else days = 28;

}
return days;
}

/**
* function: 查询某月的天数 默认是当前年份
* @param month
* @return
*/
public static int days(int month)
{
Calendar c = Calendar.getInstance();

c.setTime(new Date());

int year = c.get(Calendar.YEAR);

return days(year, month);
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式