找本java基础的入门书 一般有这个代码 以前写过 很简单不超过100行代码 带awt界面
class Date{
public static void main(String[] args){
Calendar cal=Calendar.getInstance();
SimpleDateFormat formate=new SimpleDateFormat("yyyy/MM/dd");
int year,month;
year=Integer.parseInt(request.getParameter("year"));
month=Integer.parseInt(request.getParameter("month"));//从页面得到参数年、月
if((year==null)&&(month==null)){
String nowdate=formate.format(cal.getTime());
System.out.println("the Date of today:"+nowdate);
}
else{
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month-1);
System.out.println("本月日历为:");
int m=month-1;
int i=1;
while(m=cal.get(Calendar.MONTH)){
System.out.print(cal.get(Cal.DAY_OF_MONTH)+' ');
i=i+1;
cal.add(Calendar.DAY_OF_MONTH,1);
if(i=7){
System.out.print("\n");
}
}
}
仅作参考!
我很忙 我只能告诉你用数组实现了
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class DateWin extends JFrame implements ItemListener{
Calendar cd = null;
int years;
int months;
int dates;
JTable table;
Object date[][];
Object name[]={"日","一","二","三","四","五","六"};
JComboBox year,month;
JLabel label1,label2;
JPanel panel;
DateWin(String s){
super(s);
setSize(400,300);
setLocation(120,120);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
year=new JComboBox();
//year.addItem("1990");
for (int i=0;i<30;i++){
int years=1990+i;
year.addItem(years);
}
month=new JComboBox();
for (int i=1;i<13;i++){
month.addItem(i);
}
label1=new JLabel("请选择年份:");
label2=new JLabel("请选择月份:");
panel=new JPanel();
panel.add(label1);
panel.add(year);
panel.add(label2);
panel.add(month);
Container con=getContentPane();
con.setLayout(new BorderLayout());
con.add(panel,BorderLayout.NORTH);
cd=new GregorianCalendar();
//years=Integer.parseInt(year.getSelectedItem().toString());
//months=Integer.parseInt(month.getSelectedItem().toString())-1;
//cd=new GregorianCalendar(years,months,1);
years=cd.get(Calendar.YEAR);
year.setSelectedItem(years);
months=cd.get(Calendar.MONTH)+1;
month.setSelectedItem(months);
cd.set(Calendar.DATE, 1);
int dateNumber=cd.getActualMaximum(Calendar.DATE);
int firstDay=cd.get(Calendar.DAY_OF_WEEK)-1;
int count=1;
date=new Object[6][7];
for(int i=0;i<6;i++){
for(int j=0;j<7;j++){
if(count>dateNumber)
break;
else {
if ((i*7+j)
else{
date[i][j]=count;
count++;
}
}
}
}
table=new JTable(date,name);
table.setRowHeight(15);
con.add(new JScrollPane(table),BorderLayout.CENTER);
con.validate();
validate();
year.addItemListener(this);
month.addItemListener(this);
}
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
years=Integer.parseInt(year.getSelectedItem().toString());
months=Integer.parseInt(month.getSelectedItem().toString())-1;
cd=new GregorianCalendar(years,months,1);
int dateNumber=cd.getActualMaximum(Calendar.DATE);
int firstDay=cd.get(Calendar.DAY_OF_WEEK)-1;
int count=1;
date=new Object[6][7];
for(int i=0;i<6;i++){
for(int j=0;j<7;j++){
if(count>dateNumber)
break;
else {
if ((i*7+j)
else{
date[i][j]=count;
count++;
}
}
}
}
table=new JTable(date,name);
table.setRowHeight(15);
getContentPane().removeAll();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel,BorderLayout.NORTH);
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
validate();
}
}
public class HomeWork3_1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DateWin dateWin=new DateWin("Java日历");
}
}
import java.util.*;
class ViewMonth {
int month;
int year;
ViewMonth(final int displayMonth, final int displayYear) {
month = displayMonth;
year = displayYear;
}
private String checkMonth() {
String[] months = {
"1 月" , "2 月" , "3 月",
"4 月" , "5 月" , "6 月",
"7 月" , "8 月" , "9 月",
"10 月" , "11 月" , "12 月"
};
return months[month];
}
private int checkDays() {
int[] numofDays = {
31, 28, 31,
30, 31, 30,
31, 31, 30,
31, 30, 31
};
return numofDays[month];
}
/**
* 使用此方法打印该月的日历.
*/
void printMonth() {
/* 将该月份起始处的天数留空. */
int initialSpaces = 0;
try {
/* 获取月份名称. */
String monthName = checkMonth();
System.out.println();
System.out.println("\t\t\t " + year + " 年 " + monthName );
System.out.println();
} catch (ArrayIndexOutOfBoundsException ae) {
System.out.println("超出范围 ..........");
System.exit(0);
}
GregorianCalendar cal = new GregorianCalendar(year, month, 1);
System.out.println("\t日\t一\t二\t三\t四\t五\t六");
initialSpaces = cal.get(Calendar.DAY_OF_WEEK) - 1;
/* 获取天数. */
int daysInMonth = checkDays();
/* 检查是否为闰年并为二月增加一天. */
if (cal.isLeapYear(cal.get(Calendar.YEAR)) && month == 1) {
++daysInMonth;
}
for (int ctr = 0; ctr < initialSpaces; ctr++) {
System.out.print("\t");
}
for (int ctr = 1; ctr <= daysInMonth; ctr++) {
/* 为单个日期添加空格. */
if (ctr <= 9) {
System.out.print(" ");
}
System.out.print("\t" + ctr);
/* 检查行的末尾. */
if ((initialSpaces + ctr) % 7 == 0) {
System.out.println();
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
class J7上机2 {
protected J7上机2() {
}
public static void main(String[] args) {
int month, year;
if (args.length == 2) {
System.out.println("显示日历");
System.out.println();
int mon = Integer.parseInt(args[0]);
month = mon - 1;
year = Integer.parseInt(args[1]);
} else {
Calendar today = Calendar.getInstance();
month = today.get(Calendar.MONTH);
year = today.get(Calendar.YEAR);
}
ViewMonth mv = new ViewMonth(month, year);
mv.printMonth();
}
}