求助!
编写程序,提示用户输入年号和该年的第一天的星期,在显示器上显示该年的月历,例如2005年,2005年1月1日为星期六,则显示为January 2005
---------------------------------------------
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 31
。
。
。
December 2005
--------------------------------------------
请给个思路,需要每个月都分开写吗?还有怎样用循环使日期在星期六之后换行?谢谢
[[it] 本帖最后由 tntchenmingye 于 2008-3-19 20:23 编辑 [/it]]
----------------解决方案--------------------------------------------------------
使用java.util.GorgreianCalendar这个类
它是一个日历类吧,简单地是这样说。
你查一查API,便知道他的使用方法。
----------------解决方案--------------------------------------------------------
这是一段在《JAVA核心技术》中的代码,你看看吧
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);
int month = d.get(Calendar.MONTH);
int year = d.get(Calendar.YEAR);
System.out.println(year+"年"+month+"月"+today+"日");
// set d to start date of the month
d.set(Calendar.DAY_OF_MONTH, 1);
int weekday = d.get(Calendar.DAY_OF_WEEK);
// print heading
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
// indent first line of calendar
for (int i = Calendar.SUNDAY; i < weekday; i++ )
System.out.print(" ");
do
{
// print day
int day = d.get(Calendar.DAY_OF_MONTH);
if (day < 10) System.out.print(" ");
System.out.print(day);
// mark current day with *
if (day == today)
System.out.print("* ");
else
System.out.print(" ");
// start a new line after every Saturday
if (weekday == Calendar.SATURDAY)
System.out.println();
// advance d to the next day
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
while (d.get(Calendar.MONTH) == month);
// the loop exits when d is day 1 of the next month
// print final end of line if necessary
if (weekday != Calendar.SUNDAY)
System.out.println();
}
}
----------------解决方案--------------------------------------------------------
哈哈,楼上的代码我也打过。只不过后来我改了之后就出错了!
我以前在main里改的!
----------------解决方案--------------------------------------------------------
我在用标签改呢...不怎么想改了..呵呵..
----------------解决方案--------------------------------------------------------
我写一个 看看 呵呵
import java.awt.*;import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class MyDate extends JFrame{
Calendar now;
String temp;
int day_month[] = {31,29,31,30,31,30,31,31,30,31,30,31};
int first = 0;
JComboBox jcb_month;
JTextField year_jtf;
JPanel jpanel,jpanel_date;
JTextArea jta;
JLabel year_jlabel,month_jlabel;
MyDate(){
now = Calendar.getInstance();
jpanel = new JPanel();
year_jlabel = new JLabel("年");
year_jtf = new JTextField("2008");
jcb_month = new JComboBox(new String[]{"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"});
month_jlabel = new JLabel("月");
jpanel.add(year_jtf);
jpanel.add(year_jlabel);
jpanel.add(jcb_month);
jpanel.add(month_jlabel);
jpanel_date = new JPanel();
jta = new JTextArea();
jpanel_date.add(jta);
getContentPane().add(jpanel,BorderLayout.NORTH);
getContentPane().add(jpanel_date,BorderLayout.CENTER);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int now_year = now.get(Calendar.YEAR);
int now_month = now.get(Calendar.MONTH);
jcb_month.setSelectedIndex(now_month);
now.set(Calendar.DAY_OF_MONTH,1);
int now_day_of_week = now.get(Calendar.DAY_OF_WEEK);
int row_count = 0;
temp = "日 "+"一 " + "二 "+"三 "+"四 "+"五 "+"六 "+"\n";
for(int i=1;i<now_day_of_week;i++){
temp = temp + " ";
}
for(int j=1;j<=day_month[now_month];j++){
if( j < 10)
temp = temp + j + " ";
else
temp = temp + j +" ";
row_count ++;
if(row_count + now_day_of_week == 8 || row_count == 7){
temp = temp + "\n";
row_count= 0;
now_day_of_week = 0;
}
}
jta.append(temp);
temp = "";
ItemHandler handler = new ItemHandler();
jcb_month.addItemListener(handler);
}
class ItemHandler implements ItemListener{
public void itemStateChanged(ItemEvent e){
temp = "";
jta.setText("");
int row_count = 0;
temp = "日 "+"一 " + "二 "+"三 "+"四 "+"五 "+"六 "+"\n";
int selected_month = jcb_month.getSelectedIndex();
int get_year = Integer.parseInt(year_jtf.getText().trim());
now.set(Calendar.MONTH, selected_month);
now.set(Calendar.YEAR, get_year);
now.set(Calendar.DAY_OF_MONTH,1);
int day_of_week = now.get(Calendar.DAY_OF_WEEK);
for(int i=1;i<day_of_week;i++){
temp = temp + " ";
}
for(int j=1;j<=day_month[selected_month];j++){
if( j < 10)
temp = temp + j + " ";
else
temp = temp + j +" ";
row_count ++;
if(row_count + day_of_week == 8 || row_count == 7){
temp = temp + "\n";
row_count= 0;
day_of_week = 0;
}
}
jta.append(temp);
temp = "";
}
}
public static void main(String args[]){
new MyDate();
}
}
----------------解决方案--------------------------------------------------------