import java.util.Calendarimport java.util.Dateimport 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 = 0i <weeks.lengthi++) { System.out.print(weeks[i]) for (int j = 0j <2j++) { System.out.print(" ")} } System.out.println() int weekOfday = getWeekOfday(month,1)//获得第一天的
星期 for (int i = 0i <(weekOfday - 1) * 5i++) { System.out.print(" ")} for (int i = 1i <= daysi++,weekOfday +=1) { if(weekOfday == 8) { weekOfday = 1 System.out.println()System.out.print(i) } else { System.out.print(i)} for (int j = 0j <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 = 0if(month!=2) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12:days = 31 breakcase 4: case 6: case 9: case 11:days = 30} } else { if(year%4==0 &&year%100!=0 || year%400==0) days = 29else 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)} }import java.text.*
import java.util.*
public class Test36 {
public static void main(String[] args) {
try {
SimpleDateFormat farmatter = new SimpleDateFormat("yyyy-MM-dd")
Date d1, d2
int year, month
String temp
Scanner scan = new Scanner(System.in)
System.out.print("请输入一个年月值\n年份:")
year = scan.nextInt()
System.out.print("月份:")
month = scan.nextInt()
MyCalendar.listCalendar(year, month)
System.out.print("请输入一个日期(yyyy-MM-dd):")
temp = scan.next()
d1 = farmatter.parse(temp)
System.out.println(d1.toLocaleString() + " 是 " + MyCalendar.getDayOfWeek(d1))
System.out.print("请输入两个日期(yyyy-MM-dd)\n")
System.out.print("日期1 : ")
temp = scan.next()
d1 = farmatter.parse(temp)
System.out.print("日期2 : ")
temp = scan.next()
d2 = farmatter.parse(temp)
int diff = MyCalendar.dateDiff(d1, d2)
System.out.printf("%s 和 %s 之间相隔 %d 天\n", d1.toLocaleString(), d2.toLocaleString(), diff)
}
catch (Exception e) {
e.printStackTrace()
}
}
}
class MyCalendar{
//1) 用户输入一个年份和月份,程序输出这个月的月历,每星期一行,星期天开始,星期六结束。
public static void listCalendar(int year, int month){
if(month<1 || month>12){
System.out.println(month + " 是非法的月份值,它的有效范围是 1~12")
return
}
System.out.println(" " + year + " - " + month)
System.out.println(" ---------------------------")
System.out.printf("%4s%4s%4s%4s%4s%4s%4s\n", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
try{
Calendar c = Calendar.getInstance()
c.set(year, month-1, 1)
int num = getDays(year, month) //本月天数
int i
for(i=0i<(c.get(Calendar.DAY_OF_WEEK) - 1) * 4i++){
System.out.print(" ")
}
for(i=0i<numi++){
c.set(year, month-1, i+1)
System.out.printf("%4s", i+1)
if(c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
System.out.println()
}
}
System.out.println()
}
catch(Exception e){
e.printStackTrace()
}
}
//2) 用户输入一个日期,程序显示这个日期当天是星期几。
public static String getDayOfWeek(Date d){
String[] weeks = new String[]{"星期天","星期一","星期二","星期三","星期四","星期五","星期六"}
Calendar c = Calendar.getInstance()
c.setTime(d)
return weeks[c.get(Calendar.DAY_OF_WEEK)-1]
}
//3) 用户输入两个日期,程序计算这两个日期相差的天数。
public static int dateDiff(Date d1, Date d2) throws Exception {
int i
int[] days = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
Calendar c1, c2
int y1, y2
int diff0 = 0, diff1 = 0, diff2 = 0
c1 = Calendar.getInstance()
c1.setTime(d1)
y1 = c1.get(Calendar.YEAR)
c2 = Calendar.getInstance()
c2.setTime(d2)
y2 = c2.get(Calendar.YEAR)
//计算d1日期到d1所在年份第一天相隔的天数
days[1] = isLeapYear(y1) ? 29 : 28
for(i=1i<=c1.get(Calendar.MONTH)i++){
diff1 += days[i-1]
}
//计算d2日期到d2所在年份第一天相隔的天数
days[1] = isLeapYear(y2) ? 29 : 28
for(i=1i<=c2.get(Calendar.MONTH)i++){
diff2 += days[i-1]
}
//计算d1、d2两个日期的年份相隔的天数
if(y1 <y2){
for(i=y1i<y2i++){
diff0 += isLeapYear(i) ? 366 : 365
}
}
else if(y1 >y2){
for(i=y2i<y1i++){
diff0 += isLeapYear(i) ? 366 : 365
}
diff0 *= -1
}
return diff0 + diff2 - diff1 + c2.get(Calendar.DATE) - c1.get(Calendar.DATE)
}
//判断一个年份是否是闰年
public static boolean isLeapYear(int year){
return ((year%400==0) || (year%4==0 &&year%100!=0)) ? true : false
}
//返回某月的天数
public static int getDays(int year, int month){
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
return 31
}
else if(month == 4 || month == 6 || month == 9 || month == 11){
return 30
}
else{
if(isLeapYear(year) == true){
return 29
}
else{
return 28
}
}
}
}
运行测试:
请输入一个年月值
年份:2011
月份:6
2011 - 6
---------------------------
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
请输入一个日期(yyyy-MM-dd):2011-6-15
2011-6-15 0:00:00 是 星期三
请输入两个日期(yyyy-MM-dd)
日期1 : 2009-3-10
日期2 : 2011-6-15
2009-3-10 0:00:00 和 2011-6-15 0:00:00 之间相隔 827 天
评论列表(0条)