水平排列有点蛋疼,不过还是想到办法搞定了,希望你运行的时候排列不会有问题。请接招:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class CalendarOutput {
private static Calendar cal1, cal2;
private static final String[] DAY_OF_WEEK = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
private static DateFormat df = new SimpleDateFormat("yyyy年MM月");
private static DateFormat parseDf = new SimpleDateFormat("yyyyMM");
private static Scanner sc;
public static void main(String[] args) {
cal1 = Calendar.getInstance();
cal1.set(Calendar.DATE, 1);
cal1.setFirstDayOfWeek(Calendar.SUNDAY);
cal2 = Calendar.getInstance();
cal2.set(Calendar.DATE, 1);
sc = new Scanner(System.in);
System.out.println("请输入年月(如201411):");
Date d = null;
while(null == d) {
try {
String line = sc.nextLine();
d = parseDf.parse(line);
} catch (ParseException e) {
System.out.println("日期格式不正确,请重新输入!");
d = null;
}
}
cal1.setTime(d);
cal2.set(Calendar.YEAR, cal1.get(Calendar.YEAR));
cal2.set(Calendar.MONTH, cal1.get(Calendar.MONTH));
cal2.add(Calendar.MONTH, 1);
printCal();
}
private static void printCal() {
// 先在最顶部显示年份
System.out.println(String.format("\t%s\t\t\t%s",
df.format(cal1.getTime()), df.format(cal2.getTime())));
printWeek(); // 然后输出星期日到星期一
// 先将两个月的日期保存到两个数组中
String[][] month1 = putMonth(cal1);
String[][] month2 = putMonth(cal2);
// 再将数组中的文字取出来并显示
for(int i = 0; i < month1.length; i ++) {
String[] month = month1[i];
// 逐周输出日期
for(int j = 0; j < month.length; j ++) {
System.out.print(month[j]);
}
// 如果第二个月周数不小于第一个月,则在本行再输出第二个月的日期
System.out.print("\t");
if(month2.length > i) {
for(int j = 0; j < month.length; j ++) {
System.out.print(month2[i][j]);
}
}
System.out.println();
}
// 如果第二个月周数大于第一个月,则再输出第二个月的日期(可用2月份检验)
if(month1.length < month2.length) {
int deltaMonth = month2.length - month1.length;
for(int i = 0; i < deltaMonth; i ++) {
for(int j = 0; j < 7; j ++) {
System.out.print(" ");
}
System.out.print("\t");
for(int j = 0; j < 7; j ++) {
System.out.print(month2[month1.length + i][j]);
}
}
}
}
private static String[][] putMonth(Calendar cal) {
List> listArray = new ArrayList
>();
int month = cal.get(Calendar.MONTH);
cal.set(Calendar.DATE, 1);
int startDayOfWeek1 = cal.get(Calendar.DAY_OF_WEEK);
Listlist = new ArrayList ();
// 先设置为本周属于上个月的几天,以空字符显示
cal.add(Calendar.DATE, -(startDayOfWeek1 - Calendar.SUNDAY) - 1);
for(int i = Calendar.SUNDAY; i < startDayOfWeek1; i ++) {
list.add("\t");
}
// 重新初始化为本月第一天
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, 1);
int maxDate = cal.getActualMaximum(Calendar.DATE); // 本月最多的天数(不用再去判断是否闰年了)
for(int j = 1; j <= maxDate; j ++) {
list.add(String.format("%4s", j + ""));
if(list.size() == 7) {
Listlist2 = new ArrayList (list);
listArray.add(list2); // 碰到星期六就换行
list = new ArrayList();
} else if(j == maxDate) {
listArray.add(list); // 最后一天就将最后一周添加进来
}
}
String[][] monthDates = new String[listArray.size()][7];
for(int i = 0; i < listArray.size(); i ++) {
Listitem = listArray.get(i);
for(int j = 0; j < 7; j ++) {
if(item.size() > j) { // 如果是最后一周的话可能存在下标越界
monthDates[i][j] = item.get(j);
} else { // 下标越界的时候设置为空字符串
monthDates[i][j] = "\t";
}
}
}
return monthDates;
}
private static void printWeek() {
for(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i ++) {
System.out.print(String.format("%4s", DAY_OF_WEEK[i - 1]));
}
System.out.print("\t");
for(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i ++) {
System.out.print(String.format("%4s", DAY_OF_WEEK[i - 1]));
}
System.out.println();
}
}
来个一年的,两个月并排。要是改成显示仅要两个月的,也不难。代码不长,不多解释了。
那还不简单 输出的时候不用System.out.println(); 用System.out.print();不就行了 然后自己手动分行