按照你的要求编写的Java日历验证程序如下
UI.java
import java.util.Scannerpublic 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(month<1 || month>12){
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=31break
case 4:
case 6:
case 9:
case 11:monthDay=30break
case 2:
if((year%4==0 && year%100!=0) || year%400==0){
monthDay=29
}else{
monthDay=28
}
break
}
if(day<1 || day>monthDay){
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()
}
}
运行结果
用Java实现的日历程序如下(图形界面程序)
import java.awt.BorderLayoutimport java.awt.Color
import java.awt.Font
import java.awt.GridLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.util.Calendar
import javax.swing.BorderFactory
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JPanel
public class CCI extends JFrame implements ActionListener{
JButton jb1=new JButton("<<")
JButton jb2=new JButton("<")
JButton jb3=new JButton(">")
JButton jb4=new JButton(">>")
JPanel jp1=new JPanel()
JPanel jp2=new JPanel()
JPanel jp3=new JPanel()
JPanel jp4=new JPanel()
JLabel jl1=new JLabel()
JLabel jl2=new JLabel()
JLabel[]jl=new JLabel[49]
String []week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}
Calendar c=Calendar.getInstance()
int year,month,day
int nowyear,nowmonth,nowday
CCI(){
super("简单日历")
nowyear=c.get(Calendar.YEAR)
nowmonth=c.get(Calendar.MONTH)+1
nowday=c.get(Calendar.DAY_OF_MONTH)
year=nowyear
month=nowmonth
day=nowday
String s=year+"年"+month+"月"
jl1.setForeground(Color.RED)
jl1.setFont(new Font(null,Font.BOLD,20))
jl1.setText(s)
jb1.addActionListener(this)
jb2.addActionListener(this)
jb3.addActionListener(this)
jb4.addActionListener(this)
jp1.add(jb1)jp1.add(jb2)jp1.add(jl1)jp1.add(jb3)jp1.add(jb4)
jp2.setLayout(null)
createMonthPanel()
jp2.add(jp3)
jl2.setFont(new Font(null,Font.BOLD,20))
jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日")
jp4.add(jl2)
add(jp1,BorderLayout.NORTH)
add(jp2,BorderLayout.CENTER)
add(jp4,BorderLayout.SOUTH)
setSize(500,500)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
setLocationRelativeTo(null)
setVisible(true)
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb1){
year=year-1
String s=year+"年"+month+"月"
jl1.setText(s)
jp3.removeAll()
createMonthPanel()
jp3.validate()
}
if(ae.getSource()==jb2){
if(month==1){
year=year-1
month=12
}else{
month=month-1
}
String s=year+"年"+month+"月"
jl1.setText(s)
jp3.removeAll()
createMonthPanel()
jp3.validate()
}
if(ae.getSource()==jb3){
if(month==12){
year=year+1
month=1
}else{
month=month+1
}
String s=year+"年"+month+"月"
jl1.setText(s)
jp3.removeAll()
createMonthPanel()
jp3.validate()
}
if(ae.getSource()==jb4){
year=year+1
String s=year+"年"+month+"月"
jl1.setText(s)
jp3.removeAll()
createMonthPanel()
jp3.validate()
}
}
public static void main(String[] args) {
new CCI()
}
public int getMonthDays(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31
case 2:
if ((year%4==0&&year%100!=0)||year%400==0) {
return 29
} else {
return 28
}
default:
return 30
}
}
public void createMonthPanel(){
c.set(year, month-1, getMonthDays(year,month))
int weekOfMonth=c.get(Calendar.WEEK_OF_MONTH)
if(weekOfMonth==6){
jp3.setLayout(new GridLayout(7,7))
jp3.setBounds(50, 20, 420, 350)
}else{
jp3.setLayout(new GridLayout(6,7))
jp3.setBounds(50, 20, 420, 300)
}
jp3.setBorder(BorderFactory.createEtchedBorder())
for(int i=0i<7i++){
jl[i]=new JLabel(week[i],JLabel.CENTER)
jl[i].setFont(new Font(null,Font.BOLD,20))
jl[i].setBorder(BorderFactory.createEtchedBorder())
jp3.add(jl[i])
}
c.set(year, month-1, 1)
int emptyFirst=c.get(Calendar.DAY_OF_WEEK)-1
int daysOfMonth=getMonthDays(year,month)
for(int i=6+emptyFirsti>=7i--){
int intyear=year
int intmonth=month
if(intmonth==1){
intyear=intyear-1
intmonth=12
}else{
intmonth=intmonth-1
}
int intdays=getMonthDays(intyear,intmonth)
jl[i]=new JLabel((intdays+7-i)+"",JLabel.CENTER)
jl[i].setFont(new Font(null,Font.BOLD,20))
jl[i].setForeground(Color.GRAY)
jl[i].setBorder(BorderFactory.createEtchedBorder())
jp3.add(jl[i])
}
for(int i=7+emptyFirsti<daysOfMonth+7+emptyFirsti++){
jl[i]=new JLabel((i-7-emptyFirst+1)+"",JLabel.CENTER)
jl[i].setFont(new Font(null,Font.BOLD,20))
if((i+1)%7==0 || (i+1)%7==1){
jl[i].setForeground(Color.RED)
}else if((i-7-emptyFirst+1)==nowday&&month==nowmonth&&year==nowyear)
jl[i].setForeground(Color.BLUE)
else
jl[i].setForeground(Color.BLACK)
jl[i].setBorder(BorderFactory.createEtchedBorder())
jp3.add(jl[i])
}
if(weekOfMonth==6)
for(int i=48i>=daysOfMonth+emptyFirst+7i--){
jl[i]=new JLabel((49-i)+"",JLabel.CENTER)
jl[i].setFont(new Font(null,Font.BOLD,20))
jl[i].setForeground(Color.GRAY)
jl[i].setBorder(BorderFactory.createEtchedBorder())
jp3.add(jl[i])
}
else
for(int i=41i>=daysOfMonth+emptyFirst+7i--){
jl[i]=new JLabel((42-i)+"",JLabel.CENTER)
jl[i].setFont(new Font(null,Font.BOLD,20))
jl[i].setForeground(Color.GRAY)
jl[i].setBorder(BorderFactory.createEtchedBorder())
jp3.add(jl[i])
}
}
}
运行结果
我给你贴上我在java核心技术中看到的代码吧,当然没有输入年份和月份,是按照当前时间创建的,写有我写的注释,应该能看的懂/*
* 2012年5月13日10:37:58
* 日历程序
* Function:
* 显示当前月份的日历
* 总结
* 1. 0-11分别代表1-12月
* 1-7分别代表周日-周六
* 2. 使用GregorianCalendar对象的get方法(参数)获取月,日,年等信息
* 3.
*/
import java.text.DateFormatSymbols
import java.util.*
public class CalendarTest {
public static void main(String[] args) {
//construct d as current date构造一个日期
GregorianCalendar d = new GregorianCalendar()
//获取今天是这个月的第几天
int today = d.get(Calendar.DAY_OF_MONTH) //Calendar.DAY_OF_MONTH作为参数调用,得到今天是这个月的第几天
int month = d.get(Calendar.MONTH) //月份
d.set(Calendar.DAY_OF_MONTH, 1) //设置d的日期是本月的1号
int weekDay = d.get(Calendar.DAY_OF_WEEK) //获取当天位于本星期的第几天,也就确定了星期几,值的范围是1-7
int firstDayOfWeek = d.getFirstDayOfWeek() //获取一星期的第一天,我们得到的是Calendar.SUNDAY,因为我们一星期的第一天是周日
int indent = 0 //为了定位本月第一天,定义索引
while (weekDay != firstDayOfWeek) {
//注意,月份用0-11代表1-12月,为了清晰起见,使用常量代替,下面获取月份得到的实际是当前月-1的值,所以我们要加1
//System.out.printf("当前星期的第%d天,位于当月的第%d天, 现在是%d月\n",
//weekDay, d.get(Calendar.DAY_OF_MONTH), d.get(Calendar.MONTH)+1) //Test Code
indent++//缩进个数+1
d.add(Calendar.DAY_OF_MONTH, -1)//当前天数-1,如果现在是1号,则执行本条代码后,时间变为上一个月最后一天
weekDay = d.get(Calendar.DAY_OF_WEEK) //重新获取当天位于本星期的第几天
}
String[] weekDayNames = new DateFormatSymbols().getShortWeekdays()//获取简短形式的星期字符串数组
//System.out.println(weekDayNames.length)getShortWeekdays()得到的数组的长度是8,下标为0的是没有值1为星期日...7为星期六
//注释代码1
//Java核心技术的代码
/*
do {
//System.out.printf("%4s", weekDayNames[weekDay]) //经过上面定义索引,weekDay代表的是本星期日
d.add(Calendar.DAY_OF_MONTH, 1) //天数加1
weekDay = d.get(Calendar.DAY_OF_WEEK) //重新获得weekDay的值
} while (weekDay != firstDayOfWeek) //当循环完一个星期后,这里判断不成立,退出循环
*/
//我写的代码,替换上面注释代码1
for (int i=1i<weekDayNames.lengthi++)//打印星期标题
System.out.printf("%3s\t", weekDayNames[i])//引号内是一个全角的空格,因为是中文版,不是书上英文环境,中文和空格对于不上,这里我们用\t解决
//System.out.printf("%3s ", weekDayNames[i]) //方式2
System.out.println()//换行
for (int i=1i<=indenti++)//确定一星期的一天位置,利用上面indent
System.out.printf("\t")//如用方式2,则这里内容是四个全角空格
//实现输出日期
d.set(Calendar.MONTH, month)
d.set(Calendar.DAY_OF_MONTH, 1)
do {
//print day
int day = d.get(Calendar.DAY_OF_MONTH)
System.out.printf("%3d", day)
if (day == today)
System.out.print("*")
System.out.print("\t")
d.add(Calendar.DATE, 1)//天数加1
weekDay = d.get(Calendar.DAY_OF_WEEK)//刷新weekDay
if (weekDay == firstDayOfWeek) //如果这天等于星期天则换行
System.out.println()
} while (d.get(Calendar.MONTH) == month)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)