我正在尝试使用日期填充数组,
例如,本月八月,它应该显示
Sunday Monday Tuesday Wednesday ... Saturday
null 1 2 3 ... 6
... ... ... ... ... ...
正在解决这个问题:
import java.time.LocalDate;
import java.util.Scanner;
public class buildCalendar {
String[] calendar = new String[48];
private final int firstDayOfMonth = 1;
Scanner sc = new Scanner(System.in);
int month, year;
public buildCalendar() {
System.out.println("Month: ");
month = sc.nextInt();
sc.nextLine();
System.out.println("Year: ");
year = sc.nextInt();
newCalendar(month, year);
}
public void newCalendar(int month, int year) {
LocalDate inputDate = LocalDate.of(year, month, firstDayOfMonth);
int dayOfWeek = inputDate.withDayOfMonth(firstDayOfMonth).getDayOfWeek().getValue();
// populate String array
int i = 0;
while (i <= calendar.length) {
if (i == dayOfWeek) {
for (int j = 0; j < inputDate.lengthOfMonth(); j++) {
calendar[i] = Integer.toString(i);
i++;
}
}
i++;
}
for (String string : calendar) {
System.out.println(string);
}
}
}
但如果我决定将月份更改为2016年1月,则会出错。
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
null null null null null 5 6
... ... ... ... ... ... ...
5假设为1。 你们怎么改变这个?
答案 0 :(得分:0)
因为我很无聊,所以我决定为此写一个完整的课程,但略有不同。
而不是日期数字的String[48]
数组,我为该月的几周创建了一个int[weeks][7]
二维数组,其中weeks
介于4
和{{之间1}},取决于该月的天数和该月第一天的工作日。值6
是&#34;空白&#34;一天。
代码已被增强为0
知晓,即确定一周是在星期日(例如美国)还是星期一(例如欧洲大部分地区)开始。
我还添加了一个不错的Locale
方法,该方法将在给定print()
中打印月份,并自动调整输出大小。
核心输入是标识月份的Locale
和YearMonth
。与您的代码类似的基本逻辑是以下7行:
Locale
如您所见,this.firstWeekdayOfWeek = WeekFields.of(this.locale).getFirstDayOfWeek();
DayOfWeek firstWeekdayOfMonth = this.yearMonth.atDay(1).getDayOfWeek();
int startWeekDay = (firstWeekdayOfMonth.getValue() - this.firstWeekdayOfWeek.getValue() + 7) % 7;
int endWeekDay = startWeekDay + this.yearMonth.lengthOfMonth();
this.weekdays = new int[(endWeekDay + 6) / 7][7];
for (int weekDay = startWeekDay, dayOfMonth = 1; weekDay < endWeekDay; weekDay++, dayOfMonth++)
this.weekdays[weekDay / 7][weekDay % 7] = dayOfMonth;
循环使用2个迭代器变量,一个用于2D数组中的位置(for
),另一个用于分配的日期编号({{1} })。
这就是我处理你似乎遇到的问题的方法。
以下是完整的课程:
weekDay
测试
dayOfMonth
输出
public final class CalendarMonth implements Comparable<CalendarMonth> {
private final YearMonth yearMonth;
private final Locale locale;
private final DayOfWeek firstWeekdayOfWeek;
private final int[][] weekdays;
public static CalendarMonth of(int year, int month) {
return new CalendarMonth(YearMonth.of(year, month), Locale.getDefault());
}
public static CalendarMonth of(int year, int month, Locale locale) {
Objects.requireNonNull(locale, "locale");
return new CalendarMonth(YearMonth.of(year, month), locale);
}
public static CalendarMonth of(int year, Month month) {
return new CalendarMonth(YearMonth.of(year, month), Locale.getDefault());
}
public static CalendarMonth of(int year, Month month, Locale locale) {
Objects.requireNonNull(locale, "locale");
return new CalendarMonth(YearMonth.of(year, month), locale);
}
public static CalendarMonth of(YearMonth yearMonth) {
Objects.requireNonNull(yearMonth, "yearMonth");
return new CalendarMonth(yearMonth, Locale.getDefault());
}
public static CalendarMonth of(YearMonth yearMonth, Locale locale) {
Objects.requireNonNull(yearMonth, "yearMonth");
Objects.requireNonNull(locale, "locale");
return new CalendarMonth(yearMonth, locale);
}
private CalendarMonth(YearMonth yearMonth, Locale locale) {
this.yearMonth = yearMonth;
this.locale = locale;
// Build weekdays array
this.firstWeekdayOfWeek = WeekFields.of(this.locale).getFirstDayOfWeek();
DayOfWeek firstWeekdayOfMonth = this.yearMonth.atDay(1).getDayOfWeek();
int startWeekDay = (firstWeekdayOfMonth.getValue() - this.firstWeekdayOfWeek.getValue() + 7) % 7;
int endWeekDay = startWeekDay + this.yearMonth.lengthOfMonth();
this.weekdays = new int[(endWeekDay + 6) / 7][7];
for (int weekDay = startWeekDay, dayOfMonth = 1; weekDay < endWeekDay; weekDay++, dayOfMonth++)
this.weekdays[weekDay / 7][weekDay % 7] = dayOfMonth;
}
public void print() {
// Get day names and determine width of longest name
String[] dayName = new String[7];
for (int i = 0; i < 7; i++)
dayName[i] = this.firstWeekdayOfWeek.plus(i).getDisplayName(TextStyle.FULL, this.locale);
int width = Arrays.stream(dayName).mapToInt(String::length).max().getAsInt();
// Print month name
String title = this.yearMonth.format(DateTimeFormatter.ofPattern("MMMM uuuu", this.locale));
System.out.println(rightTrim(center(title, width * 7 + 6)));
// Print day names
StringBuilder line = new StringBuilder();
for (int i = 0; i < 7; i++)
line.append(center(dayName[i], width)).append(' ');
System.out.println(rightTrim(line.toString()));
// Print day numbers
for (int[] week : this.weekdays) {
line.setLength(0);
for (int i = 0; i < 7; i++)
line.append(center((week[i] == 0 ? "" : String.format("%2d", week[i])), width)).append(' ');
System.out.println(rightTrim(line.toString()));
}
}
private static String center(String text, int width) {
if (text.length() >= width)
return text;
char[] buf = new char[width];
Arrays.fill(buf, ' ');
System.arraycopy(text.toCharArray(), 0, buf, (width - text.length() + 1) / 2, text.length());
return new String(buf);
}
private static String rightTrim(String text) {
return text.replaceFirst("\\s+$", "");
}
@Override
public String toString() {
return this.yearMonth.toString();
}
@Override
public int compareTo(CalendarMonth that) {
int cmp = this.yearMonth.compareTo(that.yearMonth);
if (cmp == 0)
cmp = this.locale.toLanguageTag().compareTo(that.locale.toLanguageTag());
return cmp;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof CalendarMonth) {
CalendarMonth other = (CalendarMonth) obj;
return (this.yearMonth.equals(other.yearMonth) &&
this.locale.equals(other.locale));
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(this.yearMonth, this.locale);
}
}
CalendarMonth.of(2016, Month.AUGUST).print();
CalendarMonth.of(2016, Month.JANUARY).print();
CalendarMonth.of(2016, Month.JANUARY, Locale.FRANCE).print();
August 2016
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
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 29 30 31
正如您所看到的,它从周一开始调整为一周,这恰好会将周数从6减少到5。