这道题是这样的,运用Calendar类,做一个万年历
样式和系统的那个日期显示的样式一样.
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14打印出来的样式就是这样的,这个问题有很多种算法,我想知道最有效率的,最好的是哪种,
对这个问题有兴趣,或者有点见解的朋友请说一下,
贴出你的代码,最好写上注释,
我们做程序的,要的就是以最少的代码来做一件事,所以,如果你有见解的话,
务必贴出你的代码,谢谢!!!!!!!这个题我自己已经做出来啦,所以分给得不多!!见谅!
样式和系统的那个日期显示的样式一样.
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14打印出来的样式就是这样的,这个问题有很多种算法,我想知道最有效率的,最好的是哪种,
对这个问题有兴趣,或者有点见解的朋友请说一下,
贴出你的代码,最好写上注释,
我们做程序的,要的就是以最少的代码来做一件事,所以,如果你有见解的话,
务必贴出你的代码,谢谢!!!!!!!这个题我自己已经做出来啦,所以分给得不多!!见谅!
解决方案 »
- 构造器内部的多态
- 后台如何处理上传多个文件?在线等
- 请问各个高手有什么学习java框架的办法么?
- 初学JAVA,请教简单基础问题 new java.awt.Point();
- 怎么让JAVA的JtextField获得光标时,输入法为日文平假名
- protected Map<String,XXX> columns = new LinkedHashMap<String,XXX>();
- JAVA 事件问题,请高手指教!
- windows上纯文本文件的最大容量是多少?
- jsp中自定义标签来遍历map,求助
- 17027错误是什么错误?
- enctype="multipart/form-data" 问题
- 我想问一下,在java一般的开发中,内部类用的多么?
哈哈,而且只要星期和日期就行啦,其他的都不要
格式就像上面那样,也和系统的那个日期显示差不多吧,
我没说清楚,吓到你们啦,不好意思
实际上就是用for循环啦,但是我想用最少的代码把它做出来
所以才来请教大家
import java.util.*;
public class CalendarTest
{
public static void main(String[] args)
{
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);
d.set(Calendar.DAY_OF_MONTH, 1);
int weekday = d.get(Calendar.DAY_OF_WEEK);
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for (int i = Calendar.SUNDAY; i < weekday; i++ )
System.out.print(" ");
do
{
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d", day);
if (day == today)
System.out.print("*");
else
System.out.print(" ");
if (weekday == Calendar.SATURDAY)
System.out.println();
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
while (d.get(Calendar.MONTH) == month);
if (weekday != Calendar.SUNDAY)
System.out.println();
}
}
怎么半JAVA,半C的都来了呀,呵呵,这个程序是我这样做的,要求是传一个年,只传一个参数,就把这一年12个的日历都打印出来
打印显示,我单独写了一个方法,用了递归,我把代码再改进一下,贴出来大家一起研究研究,
看看有没有可以改进的地方
import java.util.GregorianCalendar;
public class CalendarDemo{
public static void main(String[] args){
Cal d = new Cal();
d.display();
//d.getDate();
//d.disTitle(8);
}
}
/**
*日历
**/
class Cal{
Calendar c = new GregorianCalendar();
int toDay = c.get(c.DAY_OF_MONTH);
int month = c.get(c.MONTH);
/**
*显示所有月份的日历
**/
void display(){
for (int m = 0; m<12; m++){
this.disTitle(m);
}
}
/**
*显示标头
*/
void disTitle(int m){
System.out.println ("\n========2006年"+(m+1)+"月========");
String s = "日一二三四五六";
for (int i = 0; i<7; i++)
System.out.print(s.substring(i,i+1)+" ");
this.disDate(m);
}
/**
*显示日期
*/
void disDate(int x){
c.set(c.MONTH,x);
String msg = "\n"; c.set(c.DAY_OF_MONTH,1);
int week;
week = c.get(c.DAY_OF_WEEK)-1;
for (int i = 1; i<=week; i++)
msg+=" ";
int maxDay = c.getActualMaximum(c.DAY_OF_MONTH);
for (int i = 1; i<=maxDay; i++){
c.set(c.DAY_OF_MONTH,i);
if(i!=toDay || x != month){
if(i<10 )
msg+=" "+i+" ";
else
msg+=i+" ";
}
else
msg+=getDate()+" ";
//换行
if(c.get(c.DAY_OF_WEEK)==7)
msg+="\n";
}
System.out.println (msg);
}
/**
*标识当日期
*/
public String getDate(){
if(toDay<=10){
String s = "①②③④⑤⑥⑦⑧⑨⑩";
return s.substring((toDay-1),toDay);
}
else
return "※";
}
}
日 一 二 三 四 五 六
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 ========2006年2月========
日 一 二 三 四 五 六
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 ========2006年3月========
日 一 二 三 四 五 六
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
========2006年4月========
日 一 二 三 四 五 六
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 ========2006年5月========
日 一 二 三 四 五 六
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 ========2006年6月========
日 一 二 三 四 五 六
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
========2006年7月========
日 一 二 三 四 五 六
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 ========2006年8月========
日 一 二 三 四 五 六
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 ========2006年9月========
日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 ※ 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 ========2006年10月========
日 一 二 三 四 五 六
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 ========2006年11月========
日 一 二 三 四 五 六
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 ========2006年12月========
日 一 二 三 四 五 六
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 输出完成 (耗时 1 秒) - 正常终止
Calendar类是这样创建对象的吗??
应该是这样吧 Calendar c = Calendar.getInstance();
你的这个程序,你自己有没有跑过呀??
当然可以这么创建楼上的还活在Java1.4里吧
现在都Java1.7了
只不过不很多人都不常用而已
Calendar类是这样创建对象的吗??
应该是这样吧 Calendar c = Calendar.getInstance();
你的这个程序,你自己有没有跑过呀??
___________________________________________________哈哈,在 Calendar.getInstance() 的内部也是使用 new GregorianCalendar(); 产生的。挺佩服 qq7338367 的,在 EditPlus 下完成了这个程序。
的代码,我就马上去查了一下API,呵呵,可以这样用的,而且我也把他的代码复制下来运行了一下,完全没有问题,不过呢俺就觉得代码太长了点
如果,这个题用递归的话,该怎么做呢?
if(i<10 )
msg+=" "+i+" ";
else
msg+=i+" ";
}
else
msg+=getDate()+" ";
//换行
if(c.get(c.DAY_OF_WEEK)==7)
msg+="\n";
这一段代码,没看懂是什么意思?
前辈们说一下,谢谢
public static void main(String[] args) {
//请输入您要查询的年份
String str_year = JOptionPane.showInputDialog("Input year please");
//请输入您要查询的月份
String str_month = JOptionPane.showInputDialog("Input month please");
//取得年
int years = Integer.parseInt(str_year);
int year = years - 1900;
//取得月
int months = Integer.parseInt(str_month);
int month = months - 1;
//取得该月的第一天
Date dDate =new Date(year,month,1);
int days = daysInMonth(dDate);
//取得该月的第一天是星期几
int day = dDate.getDay();
int tDay;
System.out.println(" " + years + "年" + months + "月");
System.out.println(" ");
System.out.println("sun mon tue wen thu fri sat");
//打印1号前的空格
for(int j = 1; j <= day; j++){
System.out.print(" ");
}
//打印日期
for(int i = 1; i <= days; i++){
Date tDate = new Date(year,month,i);
tDay = tDate.getDay(); if(tDay == 0){
System.out.println();
}
if(i < 10){
System.out.print(i +" ");
}else {
System.out.print(i +" ");
}
}
}
/**
* 计算给定月份的天数
*/
public static int daysInMonth(Date date){
int days = 0;
int month = date.getMonth();
int year = date.getYear();
for(int i = 28; i <= 31; i++){
Date tDate = new Date(year,month,i);
if(tDate.getMonth() != month){
days = i - 1;
break;
}else{
days = 31;
}
}
return days;
}
}