问题描述
- java日历,其中一种情况多了一个换行,该如何解决呢?
-
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;public class rili {
public static final String JANUARY = "January";
public static final String FEBUARY = "Febuary";
public static final String MARCH = "March";
public static final String APRIL = "April";
public static final String MAY = "May";
public static final String JUN = "Jun";
public static final String JULY = "July";
public static final String AUGUST = "August";
public static final String SEPTERMBER = "Septermber";
public static final String OCTOBER = "October";
public static final String NOVEMBER = "November";
public static final String DECEMBER = "December";private int year; private int whatDayOnJanuaryOne; public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); for (int i = 0; i < n; i++) { int nian = in.nextInt(); int yue = in.nextInt(); // System.out.println(); System.out .println("|--------------------------------------------------|"); System.out.println(nian + "-" + yue); rili hd = new rili(nian); hd.printMonthOfYear(yue); System.out .println("|--------------------------------------------------|"); } } public rili(int year) { this.year = year; try { setWhatDayOnJanuaryOne(getJanuaryOne(year)); } catch (Exception e) { e.printStackTrace(); } } public void printMonthOfYear(int mon) throws Exception { if (mon < 1 || mon > 12) { return; } GregorianCalendar now = new GregorianCalendar(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(year + "-" + mon + "-01"); now.setTime(date); int month = now.get(Calendar.MONTH); now.set(Calendar.DAY_OF_MONTH, 1); int week = now.get(Calendar.DAY_OF_WEEK); System.out.println("SuntMontTuetWedtThutFritSat"); for (int i = Calendar.SUNDAY; i < week; i++) { System.out.print("t"); } while (now.get(Calendar.MONTH) == month) { int day = now.get(Calendar.DAY_OF_MONTH); if (day < 10) { System.out.print("" + day + "t"); } else { System.out.print("" + day + "t"); } if (week == Calendar.SATURDAY) { System.out.println(); } now.add(Calendar.DAY_OF_MONTH, 1); week = now.get(Calendar.DAY_OF_WEEK); } System.out.println(); } public int getJanuaryOne(int year) throws Exception { int[] weekDays = { 0, 1, 2, 3, 4, 5, 6 }; Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dt = sdf.parse(year + "-01-01"); cal.setTime(dt); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return weekDays[w]; } protected void printFebuary() { if (this.year % 4 == 0) { printLeapYear(); } else { printNonleapYear(); } } private void printLeapYear() { for (int j = 1; j <= 29; j++) { String tmp = ""; if (j == 1) { for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) { tmp = tmp + " "; } tmp = tmp + " " + j + " "; if (this.whatDayOnJanuaryOne == 6) { System.out.println(tmp); } else { System.out.print(tmp); } } else if (j > 1 && j < 29) { if ((this.whatDayOnJanuaryOne + j) % 7 == 0) { System.out.println(" " + j); } else { if (j < 10) { System.out.print(" " + j + " "); } else { System.out.print(" " + j + " "); } } } else if (j == 29) { System.out.println(" " + j); this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7); } } } private void printNonleapYear() { for (int j = 1; j <= 28; j++) { String tmp = ""; if (j == 1) { for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) { tmp = tmp + " "; } tmp = tmp + " " + j + " "; if (this.whatDayOnJanuaryOne == 6) { System.out.println(tmp); } else { System.out.print(tmp); } } else if (j > 1 && j < 28) { if ((this.whatDayOnJanuaryOne + j) % 7 == 0) { System.out.println(" " + j); } else { if (j < 10) { System.out.print(" " + j + " "); } else { System.out.print(" " + j + " "); } } } else if (j == 28) { System.out.println(" " + j); this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7); } } } protected void print30() { for (int j = 1; j <= 30; j++) { String tmp = ""; if (j == 1) { for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) { tmp = tmp + " "; } tmp = tmp + " " + j + " "; if (this.whatDayOnJanuaryOne == 6) { System.out.println(tmp); } else { System.out.print(tmp); } } else if (j > 1 && j < 30) { if ((this.whatDayOnJanuaryOne + j) % 7 == 0) { System.out.println(" " + j); } else { if (j < 10) { System.out.print(" " + j + " "); } else { System.out.print(" " + j + " "); } } } else if (j == 30) { System.out.println(" " + j); this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7); } } } protected void print31() { for (int j = 1; j <= 31; j++) { String tmp = ""; if (j == 1) { for (int k = 1; k <= this.whatDayOnJanuaryOne % 7; k++) { tmp = tmp + " "; } tmp = tmp + " " + j + " "; if (this.whatDayOnJanuaryOne == 6) { System.out.println(tmp); } else { System.out.print(tmp); } } else if (j > 1 && j < 31) { if ((this.whatDayOnJanuaryOne + j) % 7 == 0) { System.out.println(" " + j); } else { if (j < 10) { System.out.print(" " + j + " "); } else { System.out.print(" " + j + " "); } } } else if (j == 31) { System.out.println(" " + j); this.whatDayOnJanuaryOne = ((this.whatDayOnJanuaryOne + j) % 7); } } } public String getMonth(int m) { String month = ""; switch (m) { case 1: month = JANUARY; break; case 2: month = FEBUARY; break; case 3: month = MARCH; break; case 4: month = APRIL; break; case 5: month = MAY; break; case 6: month = JUN; break; case 7: month = JULY; break; case 8: month = AUGUST; break; case 9: month = SEPTERMBER; break; case 10: month = OCTOBER; break; case 11: month = NOVEMBER; break; case 12: month = DECEMBER; break; } return month; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getWhatDayOnJanuaryOne() { return whatDayOnJanuaryOne; } public void setWhatDayOnJanuaryOne(int whatDayOnJanuaryOne) { this.whatDayOnJanuaryOne = whatDayOnJanuaryOne; }
}
第一行输入一个组数n,表示显示日历的组数。 每一组数据包括年份和月份。
例如当输入2015年2月时,就会多一个换行
解决方案
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int i = 0; i < n; i++) {
int nian = in.nextInt();
int yue = in.nextInt();
// System.out.println();
System.out
.println("|--------------------------------------------------|");
System.out.println(nian + "-" + yue);
rili hd = new rili(nian);
hd.printMonthOfYear(yue);
if(i==n-1){//判断是否是最后一个
System.out
.print("|--------------------------------------------------|");
}else{
System.out
.println("|--------------------------------------------------|");
}
}
}
解决方案二:
补充一下,是28后下多的那个换行
1
2015 2
|--------------------------------------------------|
2015-2
Sun Mon Tue Wed Thu Fri Sat
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
|--------------------------------------------------|