Results 1 to 4 of 4
  1. #1
    Vizualni is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default DateFormat and Calendar

    Hello there. How are you guys?

    I hit the problem...again. This time my Calendar class is not working properly. I have two JTextFields. In one I have starting date and in second I have finishing date. Program calculates all days between those two date correctly only BUT it takes wrong month. For example:
    11.06.2010 (day.month.year) and result to me is 11.05.2010

    How can I fix that? Here is the code.
    Java Code:
    package kalendarracunjanje;
    
    import java.awt.Font;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Time;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.text.DateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class Main {
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("Volim Vikicu!");
            final JTextField kolikosati = new JTextField();
            final JTextField od = new JTextField("11.06.2010");
            final JTextField ddo = new JTextField("05.09.2010");
            JButton izracunaj = new JButton("Izracunaj!");
            final racunaj dani = new racunaj();
            final JTextArea v = new JTextArea();
            JScrollPane vrijeme = new JScrollPane(v);
    
            od.setSize(90,20);
            ddo.setSize(90,20);
            od.setLocation(20, 50);
            ddo.setLocation(180, 50);
    
            izracunaj.setSize(60,20);
            izracunaj.setLocation(115,50);
            izracunaj.setMargin(new Insets(0,0,0,0));
            izracunaj.setFont(new Font("Tahoma",Font.PLAIN,10));
            kolikosati.setSize(100,20);
            kolikosati.setLocation(100,250);
    
            vrijeme.setLocation(20,80);
            vrijeme.setSize(250,170);
    
            izracunaj.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    if(dani.namjesti(od.getText(), ddo.getText())){
                        kolikosati.setText(Long.toString(dani.sati()));
                        v.setText(dani.datumi());
                    }
                }
            });
    
            frame.setSize(300,310);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
    
            frame.getContentPane().add(kolikosati);
            frame.getContentPane().add(vrijeme);
            frame.getContentPane().add(ddo);
            frame.getContentPane().add(od);
            frame.getContentPane().add(izracunaj);
            
            frame.setLayout(null);
            frame.setVisible(true);
    
            
    
    
        }
    
    }
    class racunaj{
        private Calendar od;
        private Calendar do_;
        private DateFormat df;
        public racunaj(){
            od = Calendar.getInstance();
            do_ = Calendar.getInstance();
            df = new SimpleDateFormat("dd.MM.yyyy");
            System.out.println((do_.getTimeInMillis()-od.getTimeInMillis())/(60 * 60 * 1000));
        }
        public boolean namjesti(String prvi, String drugi){
            System.out.println(prvi + " --- "+ drugi);
            try {
                od.setTime(df.parse(prvi));
                do_.setTime(df.parse(drugi));
            } catch (ParseException ex) {return false;}
            System.out.println(od);
            return true;
    
        }
    
        public long sati(){
            return (do_.getTimeInMillis()-od.getTimeInMillis())/(1000 * 60*60);
        }
    
        public String datumi(){
              String dani="";
              Calendar tro = (Calendar) od.clone();
              dani = dani + tro.get(Calendar.DAY_OF_MONTH) +"/" + tro.get(Calendar.MONTH) + "/" + tro.get(Calendar.YEAR)+"\n";
              while (tro.before(do_)) {
              tro.add(Calendar.DAY_OF_MONTH, 1);
              dani = dani + tro.get(Calendar.DAY_OF_MONTH) +"/" + tro.get(Calendar.MONTH) + "/" + tro.get(Calendar.YEAR)+"\n";
              }
              return dani;
              }
    
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    Your question is about Calendar and SimpleDateFormat. what does setting a custom Font to a text component have to do with it? Why should volunteers here have to read through a whole heap of irrelevant code to see where you went wrong?

    Have you read the API for Calendar? You should be using the fields Calendar.JANUARY, Calendar.FEBRUARY etc, not numbers, to set the month. Months in Calendar are zero-based.

    Recommended reading:
    1. SSCCE : Java Glossary
    2. How To Ask Questions The Smart Way

    db

  3. #3
    Vizualni is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    First I would like to apologize because of my manners. Well I tought that the best way is just to throw the whole code. I was thinking should I highlight line of code that's been a problem but the problem was I didn't know what cause the problem. Now I know.

    Thanks :)

  4. #4
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    161
    Rep Power
    5

    Default hi month starts from 0

    hi
    hi month starts from 0


    http://www.kodejava.org/examples/23.html


    The Calendar.get(Calendar.MONTH) returns the month value as an integer starting from 0 as the first month and 11 as the last month. This mean January equals to 0, February equals to 1 and December equals to 11.

Similar Threads

  1. How to add a calendar????
    By zifis in forum New To Java
    Replies: 5
    Last Post: 04-07-2009, 04:04 PM
  2. calendar
    By John in forum SWT / JFace
    Replies: 12
    Last Post: 08-07-2008, 10:54 PM
  3. java.util.DateFormat not found in import
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 10:31 PM
  4. Web calendar
    By Daniel in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-27-2007, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •