所以我能够打印本月的公历,但是当我试图翻到下个月或上个月时,它似乎再次重新打印当月。请注意,通过"打印"日历,我的意思是我实际上将其格式化为看起来像我的完整日历(所有日子都像Google日历等)。此外,该计划还处于初期阶段。最终,我希望它支持在几天内添加事件,打印事件等等。
无论如何,这里有一些我可能相关的代码:
MyCalendar课程:
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
public class MyCalendar {
GregorianCalendar calendar;
String[] months;
String[] dayOfWeek;
int todayDay;
int maxDays;
static PrintMenu print = new PrintMenu();
private HashMap<MyCalendar, Event> myCalHash = new HashMap<MyCalendar, Event>();
MyCalendar(){
calendar = new GregorianCalendar(); //capture today
months = new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
dayOfWeek = new String[]{"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"};
todayDay = calendar.get(Calendar.DAY_OF_MONTH);
maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
}
public Calendar getCalendar(){
return calendar;
}
public void setCalendar(GregorianCalendar cal){
calendar = cal;
}
public Date getFirstDayOfMonth(){
return calendar.getTime();
//return calendar.get((Calendar.DAY_OF_WEEK) - 1);
}
public int getDay(){
return calendar.get(Calendar.DAY_OF_MONTH);
}
public int getMaximumDays(){
return maxDays;
}
public int getTodayDay(){
return todayDay;
}
public int getMonth(){
return calendar.get(Calendar.MONTH);
}
public int getYear(){
return calendar.get(Calendar.YEAR);
}
public void setNext(){
calendar.add(Calendar.MONTH, 1);
}
public void setPrevious(){
calendar.add(Calendar.MONTH, -1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
print.printCalendar(false);
print.printMenu();
System.out.println("I think we're done here!");
}
}
PrintMenu课程:
public class PrintMenu {
MenuHandler menu = new MenuHandler();
MyCalendar myCalendar = new MyCalendar();
void printCalendar(boolean withEvents){
int count = 0; //for formatting
int day = myCalendar.getTodayDay();
System.out.println(myCalendar.months[myCalendar.getMonth()] + " " + myCalendar.getYear());
System.out.print(myCalendar.dayOfWeek[6] + " ");
for(int i = 0; i < myCalendar.dayOfWeek.length - 1; i++){
System.out.print(myCalendar.dayOfWeek[i] + " ");
}
// int daysInMonth = myCalendar.getMaximumDays(); // 28
for(int i = 1; i <= myCalendar.dayOfWeek.length; i++){
count++;
if(!myCalendar.dayOfWeek[i].equals(myCalendar.getFirstDayOfMonth().toString().substring(0, 2))){
System.out.print(" ");
}else{
count = 0;
break;
}
}
System.out.println();
for(int i = 1; i <= myCalendar.getMaximumDays(); i++){
if(!withEvents){
if(i == day){
System.out.print("[" + i + "]");
}
if(i < 10){
System.out.print(" " + i + " ");
}else{
if(i != day){
System.out.print(i + " ");
}
}
}
else{
if(i < 10){
System.out.print(" " + i + " ");
}else{
System.out.print(i + " ");
}
}
count++;
if(count >= 7){
System.out.println();
count = 0; //reset back
}
}
}
void printMenu(){
System.out.println("-------------------------------------------------------------------");
System.out.println("Select one of the following options: ");
System.out.println("[L]oad [V]iew by [C]reate, [G]o to [E]vent list [D]elete [Q]uit");
menu.handleChoice();
printMenu();
}
}
ViewCalendar类(这是我尝试浏览日历并失败的地方)
import java.util.Calendar;
import java.util.Scanner;
public class ViewCalendar {
Scanner sc = new Scanner(System.in);
MyCalendar myCalendar = new MyCalendar();
public void whatView(){
System.out.print("[D]ay view or [M]view? ");
char userChoice = sc.next().charAt(0);
if(Character.toUpperCase(userChoice) == 'D'){ dayView(); }
else if(Character.toUpperCase(userChoice) == 'M'){ monthView(); }
else{
System.out.println("Invalid choice.");
whatView();
}
}
public void dayView(){
//print day calendar
System.out.print("[P]revious or [N]ext or [M]ain menu ? ");
char userChoice = sc.next().charAt(0);
if(Character.toUpperCase(userChoice) == 'P'){
}
else if(Character.toUpperCase(userChoice) == 'N'){
}
else if(Character.toUpperCase(userChoice) == 'M'){
return;
}
else{
System.out.println("Invalid choice.");
dayView();
}
}
public void monthView(){
//print month calendar
myCalendar.print.printCalendar(true);
System.out.print("[P]revious or [N]ext or [M]ain menu ? ");
char userChoice = sc.next().charAt(0);
if(Character.toUpperCase(userChoice) == 'P'){
myCalendar.setPrevious();
myCalendar.print.printCalendar(true);
}
else if(Character.toUpperCase(userChoice) == 'N'){
myCalendar.setNext();
myCalendar.print.printCalendar(true);
}
else if(Character.toUpperCase(userChoice) == 'M'){
return;
}
else{
System.out.println("Invalid choice.");
dayView();
}
}
}
无论如何,我希望没有太多的信息。我遵循了calendar.add()语法,但它似乎没有任何效果。 我很感激你们有任何见解!
这是MenuHandler类:
import java.util.Scanner;
public class MenuHandler {
Scanner sc = new Scanner(System.in);
ViewCalendar view = new ViewCalendar();
public void handleChoice(){
char userChoice = sc.next().charAt(0);
switch(Character.toUpperCase(userChoice)){
case 'L':
case 'V': view.whatView();
// menu.printMenu();
case 'C':
case 'G':
case 'E':
case 'D':
case 'Q': return;
}
}
}
答案 0 :(得分:0)
@hotshotennis您可以尝试使用方法set()
代替方法add()
来操纵日期。
public class MyCalendar {
.....
.....
public void setNext(){
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
}
public void setPrevious(){
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
}
.....
.....
}