Results 1 to 2 of 2
- 11-30-2011, 06:22 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Help with date compare in calendar
Hi There,
For an assignment I am building a birthdaycalendar.
I am left with one little problem though.
What I want the code to do is the following.
When one selects a month > the list shows the day in the month AND if there is a birthday in that month it adds the birthday from the text file to this day.
I have tried several things but I cannot get the thing to compare the dayListModel i to the daytoken. Can someone please help me out and put me on the right track again.
Java Code:import java.awt.*; import java.awt.event.*; import java.io.*; import java.text.*; //import java.util.Calendar; //import java.util.StringTokenizer; import java.util.*; import javax.swing.*; import javax.swing.event.*; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author eigenaar */ public class BirthdayCalendar extends JFrame implements ActionListener, ListSelectionListener{ private JComboBox monthBox; private JLabel todayIs; private JButton closeButton; private JTextField dayField; private JList dayList; private DefaultListModel dayListModel; private JButton openButton; private BufferedWriter outFile; private BufferedReader inFile; String monthList[]=(new DateFormatSymbols()).getMonths(); MyItemListener actionListener = new MyItemListener(); private void createGUI(){ setDefaultCloseOperation(EXIT_ON_CLOSE); Container window = getContentPane (); window.setLayout(new FlowLayout () ); todayIs = new JLabel (); window.add(todayIs); monthBox = new JComboBox(monthList); monthBox.setSelectedIndex(-1); window.add(monthBox); monthBox.addActionListener(this); monthBox.addItemListener(actionListener); dayListModel = new DefaultListModel(); dayList = new JList(dayListModel); dayList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); dayList.addListSelectionListener(this); window.add(new JScrollPane(dayList)); dayField = new JTextField (15); window.add(dayField); openButton = new JButton ("Open"); window.add (openButton); openButton.addActionListener(this); closeButton = new JButton("Close Program"); window.add(closeButton); closeButton.addActionListener(this); } public void valueChanged (ListSelectionEvent e){ if (e.getValueIsAdjusting() == false) { //clear the previous selection, enable new selection if (dayList.getSelectedIndex() == -1) { dayList.setEnabled(true); } else { Object item = dayList.getSelectedValue(); Object month = monthBox.getSelectedItem(); String birthdayName, birthYear; // add a new birthday birthdayName = JOptionPane.showInputDialog("De naam van de jarige"); birthYear = JOptionPane.showInputDialog("Het geboortejaar van de jarige"); //write to file try { outFile = new BufferedWriter( new FileWriter ("C:/Users/eigenaar/Documents/test/test.txt", true)); outFile.write(month + "," + item + "," + birthdayName + "," + birthYear + "\n" ); outFile.newLine(); outFile.flush(); } catch (IOException event){ JOptionPane.showMessageDialog(null,"File error: "+ event.toString()); } finally{ if (outFile != null) try { outFile.close(); } catch (IOException event) { JOptionPane.showMessageDialog(null,"File error: "+ event.toString()); } } } } } protected class MyItemListener implements ItemListener { // This method is called only if a new item has been selected. public void itemStateChanged(ItemEvent evt) { monthBox = (JComboBox)evt.getSource(); String birthDays = null; String birthDayBoy; //Read the file for the birthdays and see if there are birthdays in the selected month try { inFile = new BufferedReader(new FileReader(new File("C:/Users/eigenaar/Documents/test/test.txt"))); String line = null; //boolean foundString = false; while (( (line = inFile.readLine() ) != null)){ StringTokenizer tokens = new StringTokenizer (line, ","); while (tokens.hasMoreTokens()){ String monthToken = tokens.nextToken(); String dayToken = tokens.nextToken(); String nameToken = tokens.nextToken(); String birthyearToken = tokens.nextToken(); if(monthBox.getSelectedItem().equals(monthToken)){ birthDays = (dayToken); //System.out.println(birthDays); dayListModel.clear(); //refresh the current view if another month is selected. Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = monthBox.getSelectedIndex(); int date = 1; calendar.set(year,month, date); int days = calendar.getActualMaximum (Calendar.DAY_OF_MONTH); ArrayList birthDay = new ArrayList(); birthDay.add(birthDays); int birthDayitem = 0; // Here starts the confusion //int s; // for (int s = 0; s< birthDay; s++) // { //// Object birthDayitem = birthDay; //// System.out.println(birthDayitem); // } System.out.println(birthDay); for (int i=0; i<days; i++) { if(birthDayitem == i){ birthDayBoy = (nameToken + " " + birthyearToken + i +birthDayitem); } else{ birthDayBoy = (""); } dayListModel.addElement(date + i + birthDayBoy); } } } } // stop confusion } catch (IOException event) { JOptionPane.showMessageDialog(null,"Cannot find file"); } } } public void actionPerformed(ActionEvent e){ //Close the programm if (e.getSource() == closeButton){ System.exit(0); } } public static void main (String[] args){ BirthdayCalendar frame = new BirthdayCalendar (); frame.setSize(500,500); frame.createGUI(); frame.setVisible(true); } }
- 12-01-2011, 08:48 PM #2
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Re: Help with date compare in calendar
I managed to get the thing going a bit further. It now adds the name to the dates more or less on date compare, but now it adds the names to all the dates following the birth date until it runs into another value or the end of the month. Could someone help me with solving this problem?
Java Code:import java.awt.*; import java.awt.event.*; import java.io.*; import java.text.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; /** * * @author eigenaar */ public class BirthdayCalendar extends JFrame implements ActionListener, ListSelectionListener{ private JComboBox monthBox; private JLabel todayIs; private JButton closeButton; private JList dayList; private DefaultListModel dayListModel; private BufferedWriter outFile; private BufferedReader inFile; String monthList[]=(new DateFormatSymbols()).getMonths(); MyItemListener actionListener = new MyItemListener(); private void createGUI(){ setDefaultCloseOperation(EXIT_ON_CLOSE); Container window = getContentPane (); window.setLayout(new FlowLayout () ); todayIs = new JLabel (); window.add(todayIs); monthBox = new JComboBox(monthList); monthBox.setSelectedIndex(-1); window.add(monthBox); monthBox.addActionListener(this); monthBox.addItemListener(actionListener); dayListModel = new DefaultListModel(); dayList = new JList(dayListModel); dayList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); dayList.addListSelectionListener(this); window.add(new JScrollPane(dayList)); closeButton = new JButton("Close Program"); window.add(closeButton); closeButton.addActionListener(this); } public void valueChanged (ListSelectionEvent e){ if (e.getValueIsAdjusting() == false) { //clear the previous selection, enable new selection if (dayList.getSelectedIndex() == -1) { dayList.setEnabled(true); } else { Object item = dayList.getSelectedValue(); Object month = monthBox.getSelectedItem(); String birthdayName, birthYear; // add a new birthday birthdayName = JOptionPane.showInputDialog("De naam van de jarige"); birthYear = JOptionPane.showInputDialog("Het geboortejaar van de jarige"); //write to file try { outFile = new BufferedWriter( new FileWriter ("C:/Users/eigenaar/Documents/test/test.txt", true)); outFile.write(month + "," + item + "," + birthdayName + "," + birthYear + "\n" ); outFile.newLine(); outFile.flush(); } catch (IOException event){ JOptionPane.showMessageDialog(null,"File error: "+ event.toString()); } finally{ if (outFile != null) try { outFile.close(); } catch (IOException event) { JOptionPane.showMessageDialog(null,"File error: "+ event.toString()); } } } } } protected class MyItemListener implements ItemListener { // This method is called only if a new item has been selected. public void itemStateChanged(ItemEvent evt) { monthBox = (JComboBox)evt.getSource(); int birthDay = 0; String birthDayBoy = (""); int day = 0; //Read the file for the birthdays and see if there are birthdays in the selected month try { inFile = new BufferedReader(new FileReader(new File("C:/Users/eigenaar/Documents/test/test.txt"))); String line = null; //boolean foundString = false; while (( (line = inFile.readLine() ) != null)){ StringTokenizer tokens = new StringTokenizer (line, ","); while (tokens.hasMoreTokens()){ String monthToken = tokens.nextToken(); String dayToken = tokens.nextToken(); String nameToken = tokens.nextToken(); String birthyearToken = tokens.nextToken(); if(monthBox.getSelectedItem().equals(monthToken)){ birthDay = Integer.parseInt(dayToken); //System.out.println(birthDay); dayListModel.clear(); //refresh the current view if another month is selected. Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = monthBox.getSelectedIndex(); int date = 1; calendar.set(year,month, date); int days = calendar.getActualMaximum (Calendar.DAY_OF_MONTH); //count days in selected month for (int i=0; i<days; i++) { day = (date + i); // compare the day with the birthday date if (birthDay == day){ for (int j = 0; j < birthDay; j++){ birthDayBoy = (" " + nameToken + " " + birthyearToken); System.out.println(birthDayBoy); } } dayListModel.addElement(day + birthDayBoy); } } } } } catch (IOException event) { JOptionPane.showMessageDialog(null,"Cannot find file"); } } } public void actionPerformed(ActionEvent e){ //Close the programm if (e.getSource() == closeButton){ System.exit(0); } } public static void main (String[] args){ BirthdayCalendar frame = new BirthdayCalendar (); frame.setSize(500,500); frame.createGUI(); frame.setVisible(true); } }
Similar Threads
-
Manually setting a date using Calendar and Date
By ravl13 in forum New To JavaReplies: 3Last Post: 10-25-2011, 11:12 PM -
Compare Mysql date with current date
By gioarvan in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 04-24-2011, 05:19 PM -
Compare date input to database with current date
By hungleon88 in forum Advanced JavaReplies: 2Last Post: 11-25-2008, 08:10 AM -
Creating a Gregorian Calendar using a Date object gives date - 1
By prachi_goliwadekar in forum New To JavaReplies: 1Last Post: 05-08-2008, 08:32 PM -
Calendar.DATE
By mew in forum New To JavaReplies: 1Last Post: 01-04-2008, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks