日历我yue诗歌诗歌

import?java.util.Calendar;

import?java.util.Date;

import?java.util.Scanner;

public?class?MyCalendar

{

public?static?void?main(String[]?args)

{

Scanner?scanner?=?new?Scanner(System.in);

String?reg?=?"^(\\d+)[^\\d]+((0?[1-9])|(1[012]))$";

while(true)

{

System.out.println("输入年月(年和月用非数字隔开:如2015.1)(什么都不输入直接退出)");

String?line?=?scanner.nextLine().trim();

if("".equals(line))

{

scanner.close();

break;

}

if(!line.matches(reg))

{

continue;

}

int?year?=?Integer.parseInt(line.replaceAll(reg,?"$1"));

int?month?=?Integer.parseInt(line.replaceAll(reg,?"$2"));

System.out.println("日\t一\t二\t三\t四\t五\t六");

Calendar?calendar?=?Calendar.getInstance();

//?这个月的1号是星期几

calendar.set(year,?month?-?1,?1);

int?day?=?calendar.get(Calendar.DAY_OF_WEEK);

int?start?=?Calendar.SUNDAY;

calendar.add(Calendar.DATE,?-day?+?start);

while(start?<?day)

{

System.out.print(calendar.get(Calendar.DATE)?+?"\t");

calendar.add(Calendar.DATE,?1);

start++;

}

calendar.set(year,?month?-?1,?1);

Date?now?=?calendar.getTime();

calendar.set(year,?month,?1);

Date?next?=?calendar.getTime();

for(Date?cur?=?now;?cur.before(next);)

{

calendar.setTime(cur);

int?x?=?calendar.get(Calendar.DATE);

String?tmp?=?x?<?10?"0"?+?x?:?x?+?"";

System.out.print(tmp?+?"\t");

if(calendar.get(Calendar.DAY_OF_WEEK)?==?Calendar.SATURDAY)

{

System.out.println();

}

calendar.add(Calendar.DATE,?1);

cur?=?calendar.getTime();

}

calendar.add(Calendar.DATE,?-1);

int?to?=?calendar.get(Calendar.DAY_OF_WEEK);

int?end?=?Calendar.SATURDAY;

while(to?<?end)

{

calendar.add(Calendar.DATE,?1);

int?x?=?calendar.get(Calendar.DATE);

String?tmp?=?x?<?10?"0"?+?x?:?x?+?"";

System.out.print(tmp?+?"\t");

to++;

}

System.out.println();

}

}

}