import java.util.Calendar
public class RiLi2 {
static String[] WEEK = { "SUN", "MON", "TUS", "WEN", "THU", "FRI", "SAT" }
static String[] MONTH = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec" }
/**
* @Title: main
* @Description: 2011-10-14 qzt:打印出一个月的日历
* @param
* @return
* @throws
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar calendar = Calendar.getInstance()
RiLi2 riLi = new RiLi2()
for (int i = 1i <= 12i++) {
riLi.setDate(calendar, 2011, i)
String[] date = new String[42]
riLi.getString(calendar, date)
riLi.printString(date, WEEK, MONTH[i - 1])
}
}
private void setDate(Calendar calendar, int year, int month) {
// Calendar里面months是0-11表示12个月
calendar.set(year, month - 1, 1)
}
private void getString(Calendar calendar, String[] date) {
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)// 本月的最大天数
int firstWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1// 1号是周几
int beginNum = 0, endNum = 0
// 前面填充空格
for (int i = 0i <firstWeeki++) {
date[i] = " "
beginNum = i + 1
}
// 后面填出日历
for (int i = 0i <maxDayi++) {
String numString = ""
int j = i + 1
if (j <10) {
numString = "0" + j
} else {
numString = "" + j
}
endNum = beginNum + i
date[endNum] = numString
}
// 后面填充空格
for (int i = endNum + 1i <date.lengthi++) {
date[i] = " "
}
}
private void printString(String[] date, String[] week, String month) {
System.out.println()
System.out.println("============" + month + "============")
for (int i = 0i <week.lengthi++) {
System.out.print(week[i])
System.out.print(" ")
}
System.out.println()
for (int i = 0i <date.lengthi++) {
System.out.print(date[i])
System.out.print(" ")
if (i % 7 == 6) {
System.out.println()
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)