问题
i'm trying to make GUI that allowed people to save there shifts in a calendar. i'm trying to build my calendar using import java.uti.Calendar. during trying to print the Month it bring me back the current month 10 instead of 11 , any suggestions?
package hours_report;
import hours_report.MyPanel.MyActionListener;
import java.util.Calendar;
import java.util.Date;
public class MyCalendar {
private Calendar MyCurrentTime;
private int currentMonth;
public MyCalendar()
{
this.MyCurrentTime = Calendar.getInstance();
}
public Date getDate()
{
return MyCurrentTime.getTime();
}
public int getMonth()
{
currentMonth = MyCurrentTime.get(MyCurrentTime.MONTH);
return currentMonth;
}
}
package hours_report;
import javax.swing.*;
public class Tester {
public static void main(String[] args)
{
JFrame frame = new JFrame("Hours Report MI QA");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,700);
MyPanel p = new MyPanel();
frame.add(p);
MyCalendar cal = new MyCalendar();
System.out.println(cal.getDate());
System.out.println(cal.getMonth());
frame.setVisible(true);
}
}
回答1:
Months are indexed from 0 not 1 so 10 is November and 11 will be December.
From: Calendar returns wrong month
回答2:
The Java Calendar class numbers months from 0 (Jan) to 11 (Dec). You will have to convert this to the conventional 1-12 numbering if that's what you require.
来源:https://stackoverflow.com/questions/27036702/calendar-java-util-return-the-wrong-month