按照你的要求編寫的Java日歷驗證程序如下
創(chuàng)新互聯(lián)建站主營衡陽網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),衡陽h5成都微信小程序搭建,衡陽網(wǎng)站營銷推廣歡迎衡陽等地區(qū)企業(yè)咨詢
UI.java
import?java.util.Scanner;
public?class?UI?{
static?Scanner?sc=new?Scanner(System.in);
public?static?int?askInt(String?s){
System.out.print(s);
return?sc.nextInt();
}
public?static?void?println(String?s){
System.out.println(s);
}
}
EE.java
public?class?EE?{
public?void?validateDateCore(){
int?year?=UI.askInt("Enter?the?year:?");
int?month=UI.askInt("Enter?the?month:?");
int?day=UI.askInt("Enter?the?day:?");
if(year??1){
UI.println("The?year?is?not?a?valid?number.");
return;
}
if(month1?||?month12){
UI.println("The?month?is?not?a?valid?number.");
return;
}
int?monthDay=0;
switch(month){
case?1:
case?3:
case?5:
case?7:
case?8:
case?10:
case?12:monthDay=31;break;
case?4:
case?6:
case?9:
case?11:monthDay=30;break;
case?2:
if((year%4==0??year%100!=0)?||?year%400==0){
monthDay=29;
}else{
monthDay=28;
}
break;
}
if(day1?||?daymonthDay){
UI.println("The?day?is?not?a?valid?number.");
return;
}else{
UI.println("It?is?"+day+"/"+month+"/"+year+".");
}
}
public?static?void?main(String[]?args)?{
new?EE().validateDateCore();
}
}
運行結(jié)果
首先,控制臺輸入肯定要是形如2013-03-06這樣的格式,這是SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD")所要求的。輸入2013-03會進入異常處理提示輸入格式不正確。至于打印日歷,你可以算出該月的第一號是星期幾,然后按順序排下去,滿7天換行就是了
隨便做了一個,其實一樓網(wǎng)友說的判斷哪些30天,是否閏年什么的沒必要,看代碼:
package?com.baidu.calendar;
import?java.awt.BorderLayout;
import?java.awt.Color;
import?java.awt.FlowLayout;
import?java.awt.Font;
import?java.awt.GridLayout;
import?java.awt.event.ItemEvent;
import?java.awt.event.ItemListener;
import?java.text.DateFormat;
import?java.text.SimpleDateFormat;
import?java.util.Calendar;
import?javax.swing.JComboBox;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
import?javax.swing.border.CompoundBorder;
import?javax.swing.border.EmptyBorder;
import?javax.swing.border.LineBorder;
public?class?CalendarInterface?extends?JFrame?{
private?static?final?long?serialVersionUID?=?1L;
private?JComboBox?yearBox,?monthBox;
private?static?final?int?YEAR_RANGE?=?50;?//?年份范圍,即往前往后各推多少年
private?JPanel?topPane,?contentPane;
private?JPanel?bottomPane,?bottomTopPane,?mainPane;
private?JLabel?dateLabel;
private?boolean?init?=?false;
private?Calendar?cal;
private?static?final?Calendar?NOW?=?Calendar.getInstance();
private?static?final?String[]?DAY_OF_WEEK?=?{"星期日",?"星期一",?"星期二",
"星期三",?"星期四",?"星期五",?"星期六"};
private?DateFormat?df?=?new?SimpleDateFormat("yyyy年MM月");
private?static?final?Color?FONT_GRAY?=?new?Color(0xaa,?0xaa,?0xaa);
private?static?final?int?DATE_GRAY?=?-1;
private?static?final?int?DATE_RED?=?1;
private?static?final?int?DATE_BLACK?=?0;
public?CalendarInterface()?{
super("日歷");
cal?=?Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.SUNDAY);
}
public?void?launch()?{
setVisible(true);
setSize(600,?450);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//?主面板
contentPane?=?new?JPanel(new?BorderLayout());
contentPane.setBorder(new?EmptyBorder(5,?5,?5,?5));
contentPane.setBackground(Color.WHITE);
//?頂部選擇日期和月份的區(qū)域
topPane?=?new?JPanel(new?FlowLayout(FlowLayout.CENTER,?50,?0));?//?居中對齊,控件間隔50px
topPane.setBorder(new?EmptyBorder(0,?0,?10,?0));?//?底部空出10px
topPane.setBackground(Color.WHITE);
//?底部顯示星期條和日歷列表區(qū)域
bottomPane?=?new?JPanel(new?BorderLayout());
bottomPane.setBorder(new?LineBorder(Color.BLACK,?1));
//?星期條
bottomTopPane?=?new?JPanel(new?GridLayout(1,?7));
bottomTopPane.setBackground(new?Color(0x55,?0x55,?0x55));
//?日期列表
mainPane?=?new?JPanel();
mainPane.setLayout(new?GridLayout(0,?7));?//?列數(shù)為7,行數(shù)自動填充
for(int?i?=?Calendar.SUNDAY;?i?=?Calendar.SATURDAY;?i?++)?{
bottomTopPane.add(createDay(DAY_OF_WEEK[i?-?1]));
}
bottomPane.add(bottomTopPane,?BorderLayout.NORTH);?//?星期欄放在頂部
//?年份下拉列表
yearBox?=?new?JComboBox();
int?currentYear?=?cal.get(Calendar.YEAR);
//?年份范圍為往前往后各推50年
for(int?i?=?currentYear?-?YEAR_RANGE;?i??currentYear?+?YEAR_RANGE?+?1;?i?++)?{
yearBox.addItem(i);
}
yearBox.addItemListener(new?ItemListener()?{
@Override
public?void?itemStateChanged(ItemEvent?e)?{
if(ItemEvent.SELECTED?==?e.getStateChange())?{
reset();?//?選擇后刷新布局
}
}
});
yearBox.setSelectedIndex(YEAR_RANGE);?//?默認選中當(dāng)前年份
String?months[]?=?{"1",?"2",?"3",?"4",?"5",?"6",?"7",?"8",?"9",?"10",?"11",?"12"};
//?月份下拉列表
monthBox?=?new?JComboBox(months);
monthBox.addItemListener(new?ItemListener()?{
@Override
public?void?itemStateChanged(ItemEvent?e)?{
if(ItemEvent.SELECTED?==?e.getStateChange())?{
reset();?//刷新布局
}
}
});
monthBox.setSelectedIndex(cal.get(Calendar.MONTH));?//?選擇當(dāng)前月份
//?顯示當(dāng)前年月的控件
this.dateLabel?=?new?JLabel(df.format(cal.getTime()));
topPane.add(yearBox);
topPane.add(monthBox);
topPane.add(dateLabel);
contentPane.add(topPane,?BorderLayout.NORTH);
bottomPane.add(mainPane,?BorderLayout.CENTER);
contentPane.add(bottomPane,?BorderLayout.CENTER);
setContentPane(contentPane);
validate();
init?=?true;?//?初始化完成
reset();?//?刷新界面
setLocationRelativeTo(null);?//?居中顯示
}
public?static?void?main(String[]?args)?{
new?CalendarInterface().launch();
}
private?void?reset()?{?//?每次年份或月份改變后則日歷重新排列
if(!init)?{?//?若未初始化則返回,避免設(shè)置默認年月時調(diào)用此方法出錯
return;
}
int?year?=?(Integer)?yearBox.getSelectedItem();
int?month?=?Integer.parseInt((String)?monthBox.getSelectedItem());
//?將日期設(shè)置為本月第一天
cal.set(Calendar.YEAR,?year);
cal.set(Calendar.MONTH,?month?-?1);
cal.set(Calendar.DATE,?1);
dateLabel.setText(df.format(cal.getTime()));?//?顯示年月
mainPane.removeAll();?//?清空日歷列表
//?判斷本月第一天是星期幾,在第一天之前增加空的日歷
int?startDayOfWeek?=?cal.get(Calendar.DAY_OF_WEEK);
int?i;
//?先設(shè)置為本周屬于上個月的幾天,置灰顯示
cal.add(Calendar.DATE,?-(startDayOfWeek?-?Calendar.SUNDAY)?-?1);
for(i?=?Calendar.SUNDAY;?i??startDayOfWeek;?i?++)?{
cal.add(Calendar.DATE,?1);
int?date?=?cal.get(Calendar.DATE);
mainPane.add(createDate(Integer.toString(date),?DATE_GRAY));
}
i?=?startDayOfWeek;
//?重新初始化為本月第一天
cal.set(Calendar.MONTH,?month?-?1);
cal.set(Calendar.DATE,?1);
int?maxDate?=?cal.getActualMaximum(Calendar.DATE);?//?本月最多的天數(shù)(不用再去判斷是否閏年了)
for(int?j?=?1;?j?=?maxDate;?j?++)?{
cal.set(Calendar.DATE,?j);?//?日期一直自增,用來判斷是否是今天
JLabel?jlDate?=?null;
if(isToday())?{?//?是今天則高亮顯示
jlDate?=?createDate(Integer.toString(j),?DATE_RED);
}?else?{?//?不是今天則顯示為普通顏色
jlDate?=?createDate(Integer.toString(j));
}
mainPane.add(jlDate);
}
//?最后一周把不屬于本月的幾天用灰色控件填充
int?lastDay?=?cal.get(Calendar.DAY_OF_WEEK);
for(i?=?lastDay;?i??Calendar.SATURDAY;?i?++)?{
cal.add(Calendar.DATE,?1);
int?date?=?cal.get(Calendar.DATE);
mainPane.add(createDate(Integer.toString(date),?DATE_GRAY));
}
mainPane.validate();
}
private?JLabel?createDate(String?date)?{
return?createDate(date,?DATE_BLACK);
}
//?創(chuàng)建日期控件
private?JLabel?createDate(String?date,?int?dateColor)?{
JLabel?label?=?new?JLabel(date,?JLabel.CENTER);
CompoundBorder?border?=?new?CompoundBorder(
new?LineBorder(Color.WHITE,?1),
new?EmptyBorder(10,?10,?10,?10));
label.setBorder(border);
Font?font?=?new?Font("Arial",?Font.BOLD,?30);
if(DATE_GRAY?==?dateColor)?{?//?如果不是本月則文字置灰
label.setForeground(FONT_GRAY);
}?else?if(DATE_RED?==?dateColor)?{?//?如果是今天則高亮顯示
label.setForeground(Color.RED);
}
label.setFont(font);
return?label;
}
//?創(chuàng)建星期幾的橫條
private?JLabel?createDay(String?day)?{
JLabel?jlDay?=?new?JLabel(day,?JLabel.CENTER);
jlDay.setBorder(new?EmptyBorder(5,?5,?5,?5));
jlDay.setBackground(Color.GRAY);
jlDay.setForeground(Color.WHITE);
return?jlDay;
}
//?判斷是否今天
private?boolean?isToday()?{
return?cal.get(Calendar.YEAR)?==?NOW.get(Calendar.YEAR)
?cal.get(Calendar.MONTH)?==?NOW.get(Calendar.MONTH)
?cal.get(Calendar.DATE)?==?NOW.get(Calendar.DATE);
}
}
import java.util.Scanner; public class Wan{ public static void main(String[] args){ Scanner name = new Scanner(System.in); System.out.print("請輸入要查詢的年份:"); int year = name.nextInt(); System.out.print("請輸入該年的月份"); int month = name.nextInt(); } //累加 該年至輸入的月份 天數(shù) //比如 輸入2009年的 3月分 // 那就累加 2009年的1月至 3月1號的總天數(shù) public void sumDay(int year,int month){ int day = 0; int sumDay = 0; for(int i = 1;i=month;i++){ switch(i){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = 31; break; case 2: if(year % 4 == 0 || year % 400 == 0 year %100!=0){ day = 29; }else{ day = 28; } break; default: day = 30; } //最后一個月份不要累加 因為我們只是要算到該月的一號就可以了 if(i month){ sumDay += day; } } //累加 2000年到該年的一月一號天數(shù) for(int i = 2000;iyear;i++){ if( i % 4 == 0 || i %400== 0 i % 100 != 0){ sumDay += 366; }else{ sumDay += 365; } } //求該月一號為星期幾 int week = sumDay % 7 +1; if(week == 7){ week = 0; } } public void fomatDate(int week,int day){ int g = 0; for(int i = 0;iweek;i++){ System.out.print("\t"); } for(int i = 1;i=day;i++){ System.out.print(i+"\t"); g = week + i; if(g % 7 == 0){ System.out.println(); } } } } 給點分哈 寫得好累