在我的程序的一部分中,我通过将日期设置为用户指定的内容来操纵公历。我能够打印正确的月,日和年,但它没有显示一周中正确的日期。有人可以指出发生了什么吗?
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Map.Entry;
public class MyCalendar {
GregorianCalendar calendar;
private HashMap<GregorianCalendar, Event> myCalHash;
GregorianCalendar dayCounter = new GregorianCalendar(); // capture today
Scanner sc = new Scanner(System.in);
static MONTHS[] arrayOfMonths = MONTHS.values();
static DAYS[] arrayOfDays = DAYS.values();
MyCalendar(){
calendar = new GregorianCalendar();
myCalHash = new HashMap<GregorianCalendar, Event>();
}
public HashMap<GregorianCalendar, Event> getMyCalHash(){
return myCalHash;
}
public void setMyCalHash(HashMap<GregorianCalendar, Event> myCalHash) {
this.myCalHash = myCalHash;
}
public void viewCalendar() {
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.");
}
}
public void dayView(){
//print day calendar
//GregorianCalendar dayCounter = new GregorianCalendar(); // capture today
System.out.println(arrayOfDays[calendar.get(Calendar.DAY_OF_WEEK) - 1] + ", " + arrayOfMonths[calendar.get(Calendar.MONTH)]
+ " " + calendar.get(Calendar.DATE) + ", " + calendar.get(Calendar.YEAR));
System.out.print("[P]revious or [N]ext or [M]ain menu ? ");
char userChoice = sc.next().charAt(0);
if(Character.toUpperCase(userChoice) == 'P'){
calendar.add(Calendar.DAY_OF_MONTH, -1);
dayView();
}
else if(Character.toUpperCase(userChoice) == 'N'){
calendar.add(Calendar.DAY_OF_MONTH, 1);
dayView();
}
else if(Character.toUpperCase(userChoice) == 'M'){
return;
}
else{
System.out.println("Invalid choice.");
return;
}
}
public void monthView(){
//print month calendar
int formatCounter = 0;
dayCounter.set(Calendar.DAY_OF_MONTH, 1);
System.out.println(" " + arrayOfMonths[calendar.get(Calendar.MONTH) ] + " " + calendar.get(Calendar.YEAR)); //prints the month and year
for(int i = 0; i < arrayOfDays.length; i++){
if(i == 0){
System.out.print(arrayOfDays[i]);
}
else{
System.out.print(" " + arrayOfDays[i]);
}
}//print days of week
System.out.println();
for(int i = 0; i < arrayOfDays.length; i++){
if(!arrayOfDays[i].equals(arrayOfDays[dayCounter.get(Calendar.DAY_OF_WEEK) - 1])){
System.out.print(" ");
formatCounter++;
}
else{
System.out.print(" " + calendar.getActualMinimum(Calendar.DAY_OF_MONTH) + " ");
formatCounter++;
break;
}
}
for(int i = 1; i < dayCounter.getActualMaximum(Calendar.DAY_OF_MONTH); i++){
if(formatCounter == 7){
System.out.println();
formatCounter = 0; //reset counter
}
dayCounter.roll(Calendar.DAY_OF_MONTH, true);
if(dayCounter.get(Calendar.DATE) <= 9){
System.out.print(" " + dayCounter.get(Calendar.DATE) + " ");
formatCounter++;
}
else{
System.out.print(dayCounter.get(Calendar.DATE) + " ");
formatCounter++;
}
}
System.out.print("\n[P]revious or [N]ext or [M]ain menu ? ");
char userChoice = sc.next().charAt(0);
if(Character.toUpperCase(userChoice) == 'P'){
calendar.add(Calendar.MONTH, -1);
dayCounter.add(Calendar.MONTH, -1);
monthView();
}
else if(Character.toUpperCase(userChoice) == 'N'){
calendar.add(Calendar.MONTH, 1);
dayCounter.add(Calendar.MONTH, 1);
monthView();
}
else if(Character.toUpperCase(userChoice) == 'M'){
return;
}
else{
System.out.println("Invalid choice.");
return;
}
}
public void goTo(){
System.out.print("Enter a date in MM/DD/YYYY format: ");
String userDate = sc.nextLine();
String[] dateArr = userDate.split("/");
calendar.set(GregorianCalendar.YEAR, Integer.parseInt(dateArr[2]));
calendar.set(GregorianCalendar.MONTH, Integer.parseInt(dateArr[0]));
calendar.set(GregorianCalendar.DATE, Integer.parseInt(dateArr[1]));
System.out.println(arrayOfDays[calendar.get(Calendar.DAY_OF_WEEK) - 1] + ", " +
arrayOfMonths[calendar.get(Calendar.MONTH) - 1] + " " + calendar.get(Calendar.DATE) + ", " + calendar.get(Calendar.YEAR));
//more stuff
}
}
编辑: DAYS是在testerClass中定义的枚举,如下所示:
enum DAYS
{
Su, Mo, Tu, We, Th, Fr, Sa;
}
示例输出: 如果用户输入今天的日期(03/12/2015) 程序应打印: 2015年3月12日 但程序正在打印: 苏,2015年3月12日
另一个编辑 以下是我在enum中定义月份的方式:
enum MONTHS
{
January, February, March, April, May, June, July, August, September, October, November, December;
}
答案 0 :(得分:1)
Calendar
月份数字从零开始,例如Calendar.MARCH == 2
。您的goTo()
方法没有考虑到这一点,因此,它将日历设置为错误的月份。
<强>更新强>:
看起来你想要这个:
public void goTo(){
System.out.print("Enter a date in MM/DD/YYYY format: ");
String userDate = sc.nextLine();
String[] dateArr = userDate.split("/");
calendar.set(GregorianCalendar.YEAR, Integer.parseInt(dateArr[2]));
calendar.set(GregorianCalendar.MONTH, Integer.parseInt(dateArr[0]) - 1);
calendar.set(GregorianCalendar.DATE, Integer.parseInt(dateArr[1]));
System.out.println(arrayOfDays[calendar.get(Calendar.DAY_OF_WEEK) - 1] + ", "
+ arrayOfMonths[calendar.get(Calendar.MONTH)] + " "
+ calendar.get(Calendar.DATE) + ", "
+ calendar.get(Calendar.YEAR));
// ...
}
答案 1 :(得分:1)
John Bollinger的回答指出了你之所以看到这种异常的原因,因为月份是基于0的,而不是基于1的,而你的goTo
方法是错误地设置日期。
这里有几个问题,给你一个错误的印象,即除了星期几以外,你在所有地方正确设置了一些东西。
首先:
calendar.set(GregorianCalendar.MONTH, Integer.parseInt(dateArr[0]));
您解析用户输入并根据用户值设置日历的月份(对于&#34; 3月和#34;也称为03)。
然而,正如约翰所指出的那样,月份数字是从0开始的,所以日历认为3是&#34; 4月和#34;。
这对您的查询来说非常有用,因为您正确地获得了星期几(星期日),但错误的月份(4月),2015年的第12天。
您从数组中查找但是正确地给您的印象是月份设置正确,因为您使用了当月的日历值(基于0) )。
您错误地从此值中减去1以查找月份名称,这会让您认为星期几是错误的。
你需要修复两个地方(查找和设置):
calendar.set(GregorianCalendar.YEAR, Integer.parseInt(dateArr[2]));
calendar.set(GregorianCalendar.MONTH, Integer.parseInt(dateArr[0])-1);
calendar.set(GregorianCalendar.DATE, Integer.parseInt(dateArr[1]));
System.out.println(arrayOfDays[calendar.get(Calendar.DAY_OF_WEEK) - 1] + ", " +
arrayOfMonths[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.DATE) + ", " + calendar.get(Calendar.YEAR));