public double getNormalForMonth(int month, StatIndex stat) { GregorianCalendar calendar = new GregorianCalendar(2001, month, 1); int lastDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, lastDayOfMonth); return getNormalMonthToDate(calendar, stat); }
private boolean matchDay(Date that) { if (day == UNSPECIFIED_DAY) return true; if (day == LAST_DAY_OF_MONTH) { GregorianCalendar gc = that.calculateGC(); int lastDay = gc.getActualMaximum(Calendar.DATE); return lastDay == that.day; } return day == that.day; }
public MonthPanel() { super(ActiveView.MONTH_VIEW); GridLayout daysOfMonthLayout = new GridLayout(7, 5); this.setLayout(daysOfMonthLayout); GregorianCalendar calendar = new GregorianCalendar(); for (int di = 0; di < calendar.getActualMaximum(Calendar.DAY_OF_MONTH); di++) { JPanel day = new DayPanel(ActiveView.MONTH_VIEW, WeekDayNames.EMPTYDAY); this.add(day); } }
public static Calendar getLastDayOfMonth(Date date) { GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date); int month = cal.get(Calendar.MONTH); int year = cal.get(Calendar.YEAR); int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH); GregorianCalendar gc = new GregorianCalendar(year, month, day); Calendar monthEndDate = Calendar.getInstance(); monthEndDate.setTime(gc.getTime()); return monthEndDate; }
public static void refreshCalendar(int month, int year) { // instantiation String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int numoday, startom; // Number Of Days, Start Of Month // Allow/disallow buttons prev.setEnabled(true); next.setEnabled(true); if (month == 0 && year <= ryear) { prev.setEnabled(false); } // Cannot set an appointment back in time if (month == 11 && year >= ryear + 50) { next.setEnabled(false); } // Too early to set an appointment lmonth.setText(months[month]); // Refresh the month label (at the top) lmonth.setBounds( 160 - lmonth.getPreferredSize().width / 2, 25, 180, 25); // Re-align label with calendar cyear.setSelectedItem(String.valueOf(year)); // Select the correct year in the combo box // deletes current table for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { mcal.setValueAt(null, i, j); } } // Get first day of month and number of days GregorianCalendar cal = new GregorianCalendar(year, month, 1); numoday = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); startom = cal.get(GregorianCalendar.DAY_OF_WEEK); // Create calendar for (int i = 1; i <= numoday; i++) { int row = new Integer((i + startom - 2) / 7); int column = (i + startom - 2) % 7; mcal.setValueAt(i, row, column); } // Apply renderers Cal.setDefaultRenderer(Cal.getColumnClass(0), new tblCalendarRenderer()); }
public static void refreshCalendar(int month, int year) { // Variables String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int nod, som; // Number Of Days, Start Of Month // Allow/disallow buttons btnPrev.setEnabled(true); btnNext.setEnabled(true); if (month == 0 && year <= realYear - 10) { btnPrev.setEnabled(false); } // Too early if (month == 11 && year >= realYear + 100) { btnNext.setEnabled(false); } // Too late lblMonth.setText(months[month]); // Refresh the month label (at the top) lblMonth.setBounds( 160 - lblMonth.getPreferredSize().width / 2, 25, 180, 25); // Re-align label with calendar cmbYear.setSelectedItem(String.valueOf(year)); // Select the correct year in the combo box // Clear table for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { mtblCalendar.setValueAt(null, i, j); } } // Get first day of month and number of days GregorianCalendar cal = new GregorianCalendar(year, month, 1); nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); som = cal.get(GregorianCalendar.DAY_OF_WEEK); // Draw calendar for (int i = 1; i <= nod; i++) { int row = new Integer((i + som - 2) / 7); int column = (i + som - 2) % 7; mtblCalendar.setValueAt(i, row, column); } // Apply renderers tblCalendar.setDefaultRenderer(tblCalendar.getColumnClass(0), new tblCalendarRenderer()); }
/** * 取月末时间 * * @param date 日期 * @return date */ public static Date getMonthEnd(Date date) { int year = Integer.parseInt(FormatDate(date, "yyyy")); int month = Integer.parseInt(FormatDate(date, "MM")); int day = Integer.parseInt(FormatDate(date, "dd")); GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day, 0, 0, 0); int monthLength = calendar.getActualMaximum(calendar.DAY_OF_MONTH); String newDateStr = FormatDate(date, "yyyy") + "-" + FormatDate(date, "MM") + "-"; if (monthLength < 10) newDateStr += "0" + monthLength; else newDateStr += "" + monthLength; return stringToDateShort(newDateStr); }
public static Date yearMonthNow() { final GregorianCalendar dateCalendar = new GregorianCalendar(); dateCalendar.setTime(now()); final int lastDay = dateCalendar.getActualMaximum(Calendar.DAY_OF_MONTH); dateCalendar.set(Calendar.DAY_OF_MONTH, lastDay); dateCalendar.set(Calendar.HOUR_OF_DAY, 12); dateCalendar.set(Calendar.MINUTE, 0); dateCalendar.set(Calendar.SECOND, 0); dateCalendar.set(Calendar.MILLISECOND, 0); LOG.debug("yearMonthNow() - yearMonthNow: " + dateCalendar.getTime()); return dateCalendar.getTime(); }
public static void refreshCalendar(int month, int year) { months = new String[] { "Ιανουάριος", "Φεβρουάριος", "Μάρτιος", "Απρίλιος", "Μάιος", "Ιούνιος", "Ιούλιος", "Αύγουστος", "Σεπτέμβριος", "Οκτώβριος", "Νοέμβριος", "Δεκέμβριος" }; int nod, som; // Number Of Days, Start Of Month btnPrev.setEnabled(true); btnNext.setEnabled(true); if (month == 0 && year <= realYear - 10) { btnPrev.setEnabled(false); } // Too early if (month == 11 && year >= realYear + 100) { btnNext.setEnabled(false); } // Too late lblMonth.setText(months[month]); // Refresh the month label (at the top) lblMonth.setBounds( 160 - lblMonth.getPreferredSize().width / 2, 25, 180, 25); // Re-align label with calendar cmbYear.setSelectedItem(String.valueOf(year)); // Select the correct year in the combo box for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { mtblCalendar.setValueAt(null, i, j); } } // Get first day of month and number of days GregorianCalendar cal = new GregorianCalendar(year, month, 1); nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); som = cal.get(GregorianCalendar.DAY_OF_WEEK); for (int i = 1; i <= nod; i++) { int row = new Integer((i + som - 2) / 7); int column = (i + som - 2) % 7; mtblCalendar.setValueAt(i, row, column); } tblCalendar.setDefaultRenderer(tblCalendar.getColumnClass(0), new tblCalendarRenderer()); }
// metoda koja ispisuje godinu i mjesec public static void printCalendar(int month, int year) { System.out.println("\t\t " + monthName(month) + year); System.out.println("---------------------------------------------------"); System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat"); GregorianCalendar calendar = new GregorianCalendar(year, month - 1, 1); int start = calendar.get(GregorianCalendar.DAY_OF_WEEK); // prvi dan u sedmici for (int i = 1; i < start; i++) { // uslov za pocetak prvog dana u sedmici System.out.print("\t"); } for (int i = 1; i <= calendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++) { System.out.print(i + "\t"); if ((i + start - 1) % 7 == 0) { // uslov za ispis dana u sedmici System.out.println(); } } System.out.println("\n"); }
/** * Get a random date * * @return "YYYY-MM-DD" String */ public String randDate() { String yearString, monthString, dayString; GregorianCalendar gc = new GregorianCalendar(); int year = 2015; gc.set(Calendar.YEAR, year); int dayOfYear = randInt(1, gc.getActualMaximum(Calendar.DAY_OF_YEAR)); gc.set(Calendar.DAY_OF_YEAR, dayOfYear); yearString = Integer.toString(gc.get(Calendar.YEAR)); monthString = Integer.toString(gc.get(Calendar.MONTH) + 1); dayString = Integer.toString(gc.get(Calendar.DAY_OF_MONTH)); if (monthString.length() == 1) monthString = "0" + monthString; if (dayString.length() == 1) dayString = "0" + dayString; return yearString + "-" + monthString + "-" + dayString; }
/** * @param testY candidate year * @param testM candidate month in range 1 to 12 * @param testD candidate day in range 1 to 31 * @param leniant succeed even if the day and month might be ambiguous * @return Formatted date if certain date fields are OK and wouldn't be OK in a different order, * else null */ private String populateDateFieldsIfUnambiguous(int testY, int testM, int testD, boolean leniant) { if ((testY < 1600) || (testY > 4500)) { // Year does not fit within what Outlook allows, probably not what is intended return null; } int firstAllowableDay = leniant ? 1 : 13; if ((testD < firstAllowableDay) || (testD > 31)) { // Either an invalid date or could be a month number if guessed date string order wrong return null; } if ((testM < 1) || (testM > 12)) { // Not a valid month return null; } ICalTimeZone tzUTC = ICalTimeZone.getUTC(); GregorianCalendar cal = new GregorianCalendar(tzUTC); cal.set(testY, testM - 1, 1, 0, 0); if (testD > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) return null; cal.set(Calendar.DAY_OF_MONTH, testD); SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd"); ymd.setCalendar(cal); return ymd.format(cal.getTime()); }
/** * This is the constructor which initializes the instance variables and the components. Also at * last it will display the initial screen. * * @param m is the model and HAS-A relationship gets constructed here. */ public MonthlyView(Model m) { // CHANGING THE FONT AND SIZE java.io.InputStream is = DailyView.class.getResourceAsStream("Raleway-Regular.ttf"); try { Font font = Font.createFont(Font.TRUETYPE_FONT, is); railWay = font.deriveFont(Font.PLAIN, 12); railWayMed = font.deriveFont(Font.PLAIN, 20); railWayBigBold = font.deriveFont(Font.BOLD, 26); railWayHuge = font.deriveFont(Font.PLAIN, 100); } catch (FontFormatException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } // MAIN MONTH VIEW PANEL // -------------------------------------------------------------------- this.setLayout(new FlowLayout()); monthViewPanel = new JPanel(); monthViewPanel.setBackground(Color.WHITE); model = m; ghettoCurrDate = new int[] { MONTHS.valueOf(model.getCurrentMonth() + "").ordinal(), model.getCurrentDay(), model.getCurrentYear() }; // {month, day, // year} // (Derick) currDate = model.getCurrentDate(); temp = new GregorianCalendar(); temp.set(currDate.get(Calendar.YEAR), currDate.get(Calendar.MONTH), 1); firstdayoftheweek = temp.get(Calendar.DAY_OF_WEEK); daysInMonth = currDate.getActualMaximum(Calendar.DATE); dayButton = new JButton[31]; // UPPER HALF PANEL // -------------------------------------------------------------------------- JPanel upperPanel = new JPanel(); upperPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); upperPanel.setBackground(Color.WHITE); // CURRENT MONTH & YEAR PANEL dateLabelOfMonthPanel.setBackground(Color.WHITE); // JLabel dateLabelOfMonth = new JLabel(""); // JLabel dateLabelOfYear = new JLabel(""); dateLabelOfMonth.setText(model.getCurrentMonth() + " "); dateLabelOfYear.setText(Integer.toString(model.getCurrentYear())); dateLabelOfMonth.setFont(railWayHuge); dateLabelOfYear.setFont(railWayHuge); dateLabelOfMonth.setForeground(Color.BLACK); dateLabelOfYear.setForeground(babyTeal); dateLabelOfMonthPanel.add(dateLabelOfMonth); dateLabelOfMonthPanel.add(dateLabelOfYear); upperPanel.add(dateLabelOfMonthPanel, BorderLayout.CENTER); // PREVIOUS & NEXT BUTTON PANEL // JPanel previousNextButtonPanel = new JPanel(new GridLayout(1, 2)); // JPanel previousButtonLeftPanel = new JPanel(); // JButton previousButton = new JButton(); // try // { // Image img = ImageIO.read(getClass().getResource("pre1.png")); // previousButton.setIcon(new ImageIcon(img)); // Image newimg = img.getScaledInstance(50, 50, // java.awt.Image.SCALE_SMOOTH); // previousButton.setIcon(new ImageIcon(newimg)); // } catch (IOException ex) // { // } // previousButton.setOpaque(false); // previousButton.setBorderPainted(false); // previousButton.addActionListener(new ActionListener() // { // public void actionPerformed(ActionEvent e) // { // model.prevMonth();; // } // }); // dateLabelOfMonthPanel.add(previousButton); // previousButtonLeftPanel.add(previousButton); // previousNextButtonPanel.add(previousButtonLeftPanel, new // FlowLayout(FlowLayout.LEFT)); // upperPanel.add(previousNextButtonPanel); // // // Creates the next button // JButton nextButton = new JButton(); // try // { // Image img = ImageIO.read(getClass().getResource("next1.png")); // Image newimg = img.getScaledInstance(50, 50, // java.awt.Image.SCALE_SMOOTH); // nextButton.setIcon(new ImageIcon(newimg)); // } catch (IOException ex) // { // } // nextButton.setOpaque(false); // nextButton.setBorderPainted(false); // nextButton.setPreferredSize(new Dimension(50, 50)); // nextButton.addActionListener(new ActionListener() // { // public void actionPerformed(ActionEvent e) // { // model.nextDay(); // } // }); // JPanel daysOftheWeekPanel = new JPanel(); JLabel daysOftheWeek = new JLabel(""); daysOftheWeekPanel.setLayout(new GridLayout()); daysOftheWeekPanel.setBackground(babyTeal); String[] days = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"}; for (int i = 0; i < days.length; i++) { final String label = days[i]; JLabel dayLabel = new JLabel(label); dayLabel.setForeground(Color.BLACK); dayLabel.setBackground(lightBlue); dayLabel.setHorizontalAlignment(SwingConstants.CENTER); daysOftheWeekPanel.add(dayLabel); dayLabel.setFont(railWayMed); daysOftheWeekPanel.add(dayLabel); } dayDayPanel.setLayout(new GridLayout(0, 7)); for (int i = 1; i < firstdayoftheweek; i++) { JLabel dayLabell = new JLabel(""); dayDayPanel.add(dayLabell); } for (int i = 0; i < dayButton.length; i++) { final int counter = i + 1; GregorianCalendar foo = new GregorianCalendar(); foo.set(model.getCurrentYear(), model.getCurrentDate().get(Calendar.MONTH), i + 1); String star = ""; if (model.findEvent(foo) == true) star = " *"; dayButton[i] = new JButton(Integer.toString(i + 1) + star); dayButton[i].setOpaque(true); dayButton[i].setBorder(dayBorder); dayButton[i].setBackground(lightBlue); dayButton[i].setPreferredSize(new Dimension(205, 90)); dayButton[i].addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { daynumber = counter; model.changeDay(daynumber); DailyView.createAndShowDailyViewGUI(model); } }); } for (int i = 0; i <= daysInMonth; i++) { dayDayPanel.add(dayButton[i]); dayButton[i].setPreferredSize(new Dimension(205, 90)); dayButton[i].setBackground(Color.WHITE); dayButton[i].setHorizontalAlignment(SwingConstants.LEFT); dayButton[i].setVerticalAlignment(SwingConstants.NORTH); // set the current date to a certain color if (i + 1 == model.getCurrentDay()) { dayButton[i].setBackground(lightBlue); } } dayViewPanel = new JPanel(); dayViewPanel.setLayout(new BorderLayout()); dayLabel = new JLabel(); dayViewPanel.setBackground(Color.WHITE); dayLabel.setText( model.getCurrentDayoftheWeek() + " " + (model.getCurrentDate().get(Calendar.MONTH) + 1) + "/" + model.getCurrentDay()); // JTextArea dayViewArea = new JTextArea(20,50); ArrayList<Event> listofE = new ArrayList<Event>(); listofE = model.getEventsOfToday(); if (listofE != null) { Collections.sort(listofE); } String s = ""; for (int i = 0; i <= 24; i++) { if (i < 10) { s += ("0" + i); s += ("-------\n"); if (listofE != null) { for (Event ev : listofE) { if (ev.getStartingTime().get(GregorianCalendar.HOUR_OF_DAY) == i) { s += ev.getTitle(); s += " at " + ev.getStartingTime().get(Calendar.HOUR) + ":"; if (ev.getStartingTime().get(Calendar.MINUTE) < 10) { s += "0" + ev.getStartingTime().get(Calendar.MINUTE); } else s += ev.getStartingTime().get(Calendar.MINUTE); if (ev.getStartingTime().get(Calendar.AM_PM) == 0) { s += "AM"; } else s += "PM"; s += " to " + ev.getEndingTime().get(Calendar.HOUR) + ":"; if (ev.getEndingTime().get(Calendar.MINUTE) < 10) { s += "0" + ev.getEndingTime().get(Calendar.MINUTE); } else s += ev.getEndingTime().get(Calendar.MINUTE); if (ev.getEndingTime().get(Calendar.AM_PM) == 0) { s += "AM"; } else s += "PM"; } } } s += "\n\n"; } else { s += i; s += ("-------\n"); if (listofE != null) { for (Event ev : listofE) { if (ev.getStartingTime().get(GregorianCalendar.HOUR_OF_DAY) == i) { s += ev.getTitle(); s += " at " + ev.getStartingTime().get(Calendar.HOUR) + ":"; if (ev.getStartingTime().get(Calendar.MINUTE) < 10) { s += "0" + ev.getStartingTime().get(Calendar.MINUTE); } else s += ev.getStartingTime().get(Calendar.MINUTE); if (ev.getStartingTime().get(Calendar.AM_PM) == 0) { s += "AM"; } else s += "PM"; s += " to " + ev.getEndingTime().get(Calendar.HOUR) + ":"; if (ev.getEndingTime().get(Calendar.MINUTE) < 10) { s += "0" + ev.getEndingTime().get(Calendar.MINUTE); } else s += ev.getEndingTime().get(Calendar.MINUTE); if (ev.getEndingTime().get(Calendar.AM_PM) == 0) { s += "AM"; } else s += "PM"; } } } s += "\n\n"; } } /* * dayViewArea.setText(s); dayViewScrollPane = new * JScrollPane(dayViewArea); dayViewPanel.add(dayLabel, * BorderLayout.NORTH); dayViewPanel.add(dayViewScrollPane, * BorderLayout.CENTER); */ monthViewPanel.setLayout(new BoxLayout(monthViewPanel, BoxLayout.Y_AXIS)); monthViewPanel.add(dateLabelOfMonthPanel); monthViewPanel.add(daysOftheWeekPanel); monthViewPanel.add(dayDayPanel); monthViewPanel.setAlignmentX(Component.CENTER_ALIGNMENT); dayDayPanel.setBackground(Color.WHITE); add(monthViewPanel); }
/** Create the frame. */ public GUICalendar() { setResizable(false); eventManager = new EventManager(); this.initCalendar = eventManager.getCalendar(); eventManager.addChangeListener(this); // load event.txt by default. try { load(); } catch (IOException e1) { e1.printStackTrace(); } // eventManager.eventList(); Month = arrayOfMonths[initCalendar.get(Calendar.MONTH)]; Date = initCalendar.get(Calendar.DAY_OF_MONTH); Year = initCalendar.get(Calendar.YEAR); // puts dates of this month into the array GregorianCalendar temp = new GregorianCalendar(initCalendar.get(Calendar.YEAR), initCalendar.get(Calendar.MONTH), 1); int[] arr = new int[42]; int firstDayOfWeek = temp.get(Calendar.DAY_OF_WEEK) - 1; int lastDayOfMonth = temp.getActualMaximum(Calendar.DAY_OF_MONTH); for (int i = 0; i < arr.length; i++) { arr[i] = i + 1 + (firstDayOfWeek * -1); } setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 650, 320); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel panel = new JPanel(); panel.setBackground(new Color(255, 255, 255)); contentPane.add(panel, BorderLayout.NORTH); JButton button = new JButton("<"); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { eventManager.setCalendar(-1); // System.out.print(eventManager.getCalendar().get(Calendar.DAY_OF_MONTH)); } }); panel.add(button); JButton button_1 = new JButton(">"); button_1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { eventManager.setCalendar(1); // System.out.print(eventManager.getCalendar().get(Calendar.DAY_OF_MONTH)); } }); panel.add(button_1); JButton btnQuit = new JButton("QUIT"); btnQuit.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { quit(); } catch (IOException e) { e.printStackTrace(); } } }); btnQuit.setFont(new Font("SansSerif", Font.PLAIN, 11)); panel.add(btnQuit); JPanel panel_1 = new JPanel(); panel_1.setBackground(new Color(255, 255, 255)); contentPane.add(panel_1, BorderLayout.WEST); panel_1.setLayout(new BorderLayout(0, 0)); JButton btnNewButton = new JButton("CREATE"); btnNewButton.setBorder(UIManager.getBorder("Button.border")); btnNewButton.setPreferredSize(new Dimension(71, 30)); btnNewButton.setFont(new Font("SansSerif", Font.PLAIN, 11)); btnNewButton.addMouseListener( new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { CreateDialog dialog = new CreateDialog(eventManager.getCalendar(), dayPanel, eventManager); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); } }); btnNewButton.setForeground(new Color(255, 255, 255)); btnNewButton.setBackground(new Color(220, 20, 60)); panel_1.add(btnNewButton, BorderLayout.NORTH); label = new Label(Month + " " + Year); label.setAlignment(Label.CENTER); panel_1.add(label, BorderLayout.CENTER); monthPanel = new MonthPanel(initCalendar, eventManager); panel_1.add(monthPanel, BorderLayout.SOUTH); String date = String.format("%02d", initCalendar.get(Calendar.MONTH) + 1) + "/" + String.format("%02d", initCalendar.get(Calendar.DAY_OF_MONTH)) + "/" + initCalendar.get(Calendar.YEAR); ArrayList<String> eventList = eventManager.eventPrinter(date); dayPanel = new DayPanel(initCalendar, eventList); dayPanel.setBackground(new Color(255, 255, 255)); dayPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); contentPane.add(dayPanel, BorderLayout.CENTER); setVisible(true); }
/** * * 해당년월의 마지막 날짜 * * @param nYear : 년도 * @param nMonth : 월 * @return int */ public static int getLastDay(int nYear, int nMonth) { GregorianCalendar cld = new GregorianCalendar(nYear, nMonth - 1, 1); int result = cld.getActualMaximum(Calendar.DAY_OF_MONTH); return result; }
/** * 取的每月天數 * * @param year * @param mon * @return */ public static int getDaysInMonth(int year, int mon) { GregorianCalendar gregorianCalendar = new java.util.GregorianCalendar(year, mon - 1, 1); return (gregorianCalendar.getActualMaximum(Calendar.DATE)); }
/** * When there is a change in the model stateChanged gets notified of a changeEvent and * stateChanged is responsible for repainting the screen with the changes that has been applied. */ public void stateChanged(ChangeEvent c) { currDate = model.getCurrentDate(); temp = (GregorianCalendar) currDate.clone(); dateLabelOfMonth.setText(model.getCurrentMonth() + " "); dateLabelOfYear.setText(Integer.toString(model.getCurrentYear())); dateLabelOfMonth.setFont(railWayHuge); dateLabelOfYear.setFont(railWayHuge); dateLabelOfMonth.setForeground(Color.BLACK); dateLabelOfYear.setForeground(babyTeal); temp.set(currDate.get(Calendar.YEAR), currDate.get(Calendar.MONTH), 1); firstdayoftheweek = temp.get(Calendar.DAY_OF_WEEK); daysInMonth = currDate.getActualMaximum(Calendar.DATE); removeAll(); monthViewPanel.setLayout(new BoxLayout(monthViewPanel, BoxLayout.Y_AXIS)); monthViewPanel.add(dateLabelOfMonthPanel); monthViewPanel.add(daysOftheWeekPanel); dayDayPanel.removeAll(); for (int i = 1; i < firstdayoftheweek; i++) { JLabel dayLabell = new JLabel(""); dayDayPanel.add(dayLabell); } GregorianCalendar foo = new GregorianCalendar(); for (int i = 0; i < daysInMonth; i++) { dayButton[i].setPreferredSize(new Dimension(205, 90)); dayDayPanel.add(dayButton[i]); foo.set(model.getCurrentYear(), model.getCurrentDate().get(Calendar.MONTH), i + 1); String star = ""; if (model.findEvent(foo) == true) star = " *"; // set the current date to a certain color dayButton[i].setBackground(Color.WHITE); if (currDate.get(Calendar.MONTH) == ghettoCurrDate[0] && i + 1 == ghettoCurrDate[1] && model.getCurrentYear() == ghettoCurrDate[2]) dayButton[i].setBackground(lightBlue); dayButton[i].setText(Integer.toString(i + 1) + star); dayButton[i].setForeground(Color.BLACK); } dayButton[model.getCurrentDay() - 1].setForeground(babyTeal); dayLabel.setText( model.getCurrentDayoftheWeek() + " " + (model.getCurrentDate().get(Calendar.MONTH) + 1) + "/" + model.getCurrentDay()); // ArrayList<Event> listofE; // String s = ""; // if (model.findEvent() == true) { // // listofE = model.getEventsOfToday(); // Collections.sort(listofE); // for (int i = 0; i <= 24; i++) { // if (i < 10) { // s += ("0" + i); // s += ("-------\n"); // // for (Event ev : listofE) { // if (ev.getStartingTime().get( // GregorianCalendar.HOUR_OF_DAY) == i) { // s += ev.getTitle(); // s += " at " // + ev.getStartingTime().get(Calendar.HOUR) // + ":"; // if (ev.getStartingTime().get(Calendar.MINUTE) < 10) { // s += "0" // + ev.getStartingTime().get( // Calendar.MINUTE); // } else // s += ev.getStartingTime().get(Calendar.MINUTE); // if (ev.getStartingTime().get(Calendar.AM_PM) == 0) { // s += "AM"; // } else // s += "PM"; // // s += " to " + ev.getEndingTime().get(Calendar.HOUR) // + ":"; // if (ev.getEndingTime().get(Calendar.MINUTE) < 10) { // s += "0" // + ev.getEndingTime().get( // Calendar.MINUTE); // } else // s += ev.getEndingTime().get(Calendar.MINUTE); // if (ev.getEndingTime().get(Calendar.AM_PM) == 0) { // s += "AM"; // } else // s += "PM"; // } // } // s += "\n\n"; // } else { // s += i; // s += ("-------\n"); // // for (Event ev : listofE) { // if (ev.getStartingTime().get( // GregorianCalendar.HOUR_OF_DAY) == i) { // s += ev.getTitle(); // s += " at " // + ev.getStartingTime().get(Calendar.HOUR) // + ":"; // if (ev.getStartingTime().get(Calendar.MINUTE) < 10) { // s += "0" // + ev.getStartingTime().get( // Calendar.MINUTE); // } else // s += ev.getStartingTime().get(Calendar.MINUTE); // if (ev.getStartingTime().get(Calendar.AM_PM) == 0) { // s += "AM"; // } else // s += "PM"; // // s += " to " + ev.getEndingTime().get(Calendar.HOUR) // + ":"; // if (ev.getEndingTime().get(Calendar.MINUTE) < 10) { // s += "0" // + ev.getEndingTime().get( // Calendar.MINUTE); // } else // s += ev.getEndingTime().get(Calendar.MINUTE); // if (ev.getEndingTime().get(Calendar.AM_PM) == 0) { // s += "AM"; // } else // s += "PM"; // // } // } // s += "\n\n"; // } // } // dayViewArea.setText(s); // // } else { // for (int i = 0; i <= 24; i++) { // if (i < 10) { // s += ("0" + i); // s += ("-------\n"); // s += "\n\n"; // } else { // s += i; // s += ("-------\n"); // s += "\n\n"; // } // } // dayViewArea.setText(s); // } // dayViewPanel.add(dayLabel, BorderLayout.NORTH); // dayViewPanel.add(dayViewScrollPane, BorderLayout.CENTER); monthViewPanel.add(dayDayPanel); add(monthViewPanel, BorderLayout.CENTER); // add(dayViewPanel, BorderLayout.CENTER); repaint(); revalidate(); }
private void refreshCalendar() { if (monthCalendar != null) { monthCalendar.removeAll(); remove(monthCalendar); revalidate(); repaint(); } monthCalendar = new JPanel(); GridBagConstraints c = new GridBagConstraints(); c.weightx = 1.0; c.weighty = 1.0; Utils.setGridBagConstraints(c, 0, 1, 7, 1, GridBagConstraints.HORIZONTAL); add(monthCalendar, c); monthCalendar.setLayout(new GridLayout(0, 7)); int numberOfDays = calendar.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); GregorianCalendar g = (GregorianCalendar) calendar.clone(); g.set(GregorianCalendar.DATE, 1); int weekStartDelay; /* Here we assume the week starts on a monday. TODO: support for different locales */ switch (g.get(GregorianCalendar.DAY_OF_WEEK)) { case GregorianCalendar.MONDAY: weekStartDelay = 1; break; case GregorianCalendar.TUESDAY: weekStartDelay = 2; break; case GregorianCalendar.WEDNESDAY: weekStartDelay = 3; break; case GregorianCalendar.THURSDAY: weekStartDelay = 4; break; case GregorianCalendar.FRIDAY: weekStartDelay = 5; break; case GregorianCalendar.SATURDAY: weekStartDelay = 6; break; case GregorianCalendar.SUNDAY: weekStartDelay = 7; break; default: weekStartDelay = 0; break; } int activeWeekColorCountdown = 0; GregorianCalendar endActiveWeek = (GregorianCalendar) Client.activeWeek.clone(); endActiveWeek.add(GregorianCalendar.DATE, 7); int labelCounter = 0; for (int i = 1; i < weekStartDelay; i++) { JLabel blank = new JLabel(" "); monthCalendar.add(blank); // adding empty JLabels for padding // because I have no idea how to // actually use Swing :3 labelCounter++; } for (int i = 1; i <= numberOfDays; i++) { if (Client.activeWeek.get(GregorianCalendar.DATE) == i && Client.activeWeek.get(GregorianCalendar.MONTH) == calendar.get(GregorianCalendar.MONTH) && Client.activeWeek.get(GregorianCalendar.YEAR) == calendar.get(GregorianCalendar.YEAR)) { switch (Client.activeWeek.get(GregorianCalendar.DAY_OF_WEEK)) { case GregorianCalendar.MONDAY: activeWeekColorCountdown = 7; break; case GregorianCalendar.TUESDAY: activeWeekColorCountdown = 6; break; case GregorianCalendar.WEDNESDAY: activeWeekColorCountdown = 5; break; case GregorianCalendar.THURSDAY: activeWeekColorCountdown = 4; break; case GregorianCalendar.FRIDAY: activeWeekColorCountdown = 3; break; case GregorianCalendar.SATURDAY: activeWeekColorCountdown = 2; break; case GregorianCalendar.SUNDAY: activeWeekColorCountdown = 1; break; default: activeWeekColorCountdown = 0; break; } } JLabel day = new JLabel("" + i); if (activeWeekColorCountdown > 0) { day.setBackground(new Color(0.7f, 0.85f, 0.94f)); activeWeekColorCountdown--; } day.setPreferredSize(GUI_CONSTANTS.miniCalendarViewLabelDimension); day.setOpaque(true); monthCalendar.add(day); labelCounter++; day.addMouseListener( mouseListenerGenerator( calendar.get(GregorianCalendar.YEAR), calendar.get(GregorianCalendar.MONTH), i)); } /* for (int i = 0; i <= labelCounter; i++) { JLabel blank = new JLabel(" "); if(activeWeekColorCountdown>0){ blank.setBackground(Color.BLUE); activeWeekColorCountdown--; } monthCalendar.add(blank); //adding empty JLabels for padding } */ revalidate(); repaint(); monthCalendar.repaint(); }
/** * Assemble a CalendarWindow object for the month the given GregorianCalendar object specifies. */ CalendarWindow(GregorianCalendar date) { this.setLayout(new BorderLayout(5, 5)); this.setSize(300, 240); // retrieve symbols for day and month names // side effect: script is locale ready assuming arabic numerals DateFormatSymbols symbols = new DateFormatSymbols(); String[] weekday_symbols = symbols.getWeekdays(); String[] month_symbols = symbols.getMonths(); // set the title to something useful this.setTitle( String.format( "%s %s %01d, %d", weekday_symbols[date.get(date.DAY_OF_WEEK)], month_symbols[date.get(date.MONTH)], date.get(date.DATE), date.get(date.YEAR))); JPanel weekday_labels = new JPanel(new GridLayout(0, 7, 5, 5)); // populate the weekday names row for (int i = 1; i < weekday_symbols.length; i++) { JLabel weekday = new JLabel(weekday_symbols[i]); weekday.setHorizontalAlignment(JLabel.CENTER); weekday.setVerticalAlignment(JLabel.BOTTOM); weekday_labels.add(weekday); } JPanel day_grid = new JPanel(new GridLayout(0, 7, 5, 5)); // retrieve the weekday identifier for the first of the month. // why there isn't a function that grabs this vital information // I do not know. int first = new GregorianCalendar(date.get(date.YEAR), date.get(date.MONTH), 1).get(date.DAY_OF_WEEK); // retrieve the days in the month // The ACTUAL maximum because I really want the length of // the month, and not the maximum value the field holds. // // for the future: how about getMaximum(DAY_OF_MONTH) for // length of month and getMaximumValue(DAY_OF_MONTH) for // the maximum value the field supports. int days_in_month = date.getActualMaximum(date.DAY_OF_MONTH); // populate the day of month grid // loops from 1 to the number of days in the month, plus an // offset some for the weekday offset. for (int i = 1; i < days_in_month + first; i++) { // offset the labels so the first fall on the correct // weekday if (i - first + 1 > 0) { JLabel day; day = new JLabel("" + (i - first + 1)); if (i - first + 1 == date.get(date.DAY_OF_MONTH)) { day.setForeground(Color.RED); } day.setHorizontalAlignment(JLabel.CENTER); day_grid.add(day); } else { // placeholder, since you can't skip cells. JPanel empty = new JPanel(); day_grid.add(empty); } } // Add everything to the window. All two of them. // weekdays on a border because those collapse, days in center // because it expands. this.add(weekday_labels, BorderLayout.NORTH); this.add(day_grid, BorderLayout.CENTER); }