Java做日历,日历格式参考Windows系统托盘的日期和时间程序。

开发一个Java Application,允许用户通过命令行输入需要查询的年、月,在控制台中格式化输出该月份的日历,日历格式参考Windows系统托盘的日期和时间程序。 求大神帮忙 邮箱465014677@qq

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Game {

public void theCalendar(String str) throws ParseException {
Date date = DateFormat.getDateInstance().parse(str);
Calendar c = new GregorianCalendar();
c.setTime(date);
int year = c.get(Calendar.YEAR); // 返回年
int month = c.get(Calendar.MONTH); // 返回月
int today = c.get(Calendar.DAY_OF_MONTH);// 返回日
System.out.println("日\t一\t二\t三\t四\t五\t六");

c.set(Calendar.DAY_OF_MONTH, 1); //把当前日期设置为1号
int a = c.get(Calendar.DAY_OF_WEEK);//返回当前月份1号是星期几
for(int i=1; i<a; i++) {
System.out.print("\t");
}

while(c.get(Calendar.MONTH) == month) {
if(c.get(Calendar.DAY_OF_MONTH) == today) {
System.out.printf("%2d●\t", c.get(Calendar.DAY_OF_MONTH));
} else {
System.out.printf("%2d\t", c.get(Calendar.DAY_OF_MONTH));
}

if(c.get(Calendar.DAY_OF_WEEK) == 7) {
System.out.println();
}
c.add(Calendar.DAY_OF_MONTH, 1);
}

}

public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.println("请按照如下格式输入字符串1990-09-07>>>>>>>");
String str = sc.nextLine();

new Game().theCalendar(str);
}
}追问

谢大神O(∩_∩)O~

谢大神O(∩_∩)O~

追答

不客气

温馨提示:答案为网友推荐,仅供参考