Results 1 to 9 of 9
  1. #1
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default Can't get my date math to work

    In my app I am putting up today's day, asking for an integer, then adding that integer to today's date, simple enough. So, why doesn't it work?

    In my test I add 5 days to today, the date does change properly, but the month changes as well - and it shouldn't!

    Today is 6/12/2010, add 5 and I should get 6/17/2010, what I get is 5/17/2010.

    Application window:


    Java Code:
    calcButton.addActionListener(new ActionListener()
    {
    	public void actionPerformed(ActionEvent event)
    	{
    		try
    		{
    			DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
    			Date date = (Date)formatter.parse(startDateFld.getText());
    			startDate.setTime(date);
    		} catch (ParseException e)
    			{ System.out.println ("Exception: " + e); }
    
    		int days = Integer.parseInt(daysFld.getText());
    
    		endDate = startDate;
    		endDate.add(Calendar.DAY_OF_YEAR, 5);
    
    		endDateFld.setText(endDate.get(Calendar.MONTH) + "/"
    			+ endDate.get(Calendar.DATE) + "/"
    			+ endDate.get(Calendar.YEAR));
    	}
    });
    .
    .
    .
    Calendar startDate = Calendar.getInstance();
    Calendar endDate   = startDate;

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Get rid of the Swing code, and create a very small program with a single main method that takes the typical Strings from your GUI, excerpts from the code above, and demonstrate your problem with this small compilable program. Let's try to isolate the error.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    For instance,
    Java Code:
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class DateFu {
    
       public static void main(String[] args) {
          DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
          String startDateStr = "06/12/2010";
          Calendar startDate = Calendar.getInstance();
          try {
             Date date = formatter.parse(startDateStr);
             startDate.setTime(date);
    
             int days = 5;
    
             Calendar endDate = Calendar.getInstance();
             endDate.setTime(startDate.getTime());
             endDate.add(Calendar.DAY_OF_YEAR, days);
             System.out.println("Start date: " + formatter.format(startDate.getTime()));
             System.out.println("End date:   " + formatter.format(endDate.getTime()));
          } catch (ParseException e) {
             e.printStackTrace();
          }
       }
    }

  4. #4
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Ok, slower than you, but here's my code:

    Java Code:
    import java.awt.*;
    import java.text.*;
    import java.util.Calendar;
    import java.util.Date;
    
    public class DateTest {
    	public static void main(String[] args)
    	{
    		try
    		{
    			DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
    			Date date = (Date)formatter.parse(startDateFld);
    			startDate.setTime(date);
    		} catch (ParseException e)
    			{ System.out.println ("Exception: " + e); }
    
    		int days = 5;
    
    		endDate = startDate;
    		endDate.add(Calendar.DAY_OF_YEAR, days);
    
    		endDateFld = (endDate.get(Calendar.MONTH) + "/"
    			+ endDate.get(Calendar.DATE) + "/"
    			+ endDate.get(Calendar.YEAR));
    			
    		System.out.println (startDateFld + "\n\n" + endDateFld);
    	}
    	static String startDateFld = "06/12/2010";
    	static String endDateFld;
    	static Calendar startDate = Calendar.getInstance();
    	static Calendar endDate   = startDate;
    }
    Results:
    Java Code:
    [new-host-3:~/Documents/Java Source/Test] mike% java DateTest
    06/12/2010
    
    5/17/2010

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Ah, you need to know that Calendar uses 0 as the base for months. So January is 0, February is 1, March 2, April 3,.... etc.

    So instead, do what I do above: use the date formatter to format the output. Here we use the format method, not the parse method:

    Java Code:
    endDateFld = formatter.format(endDate.getTime());
    Last edited by Fubarable; 07-13-2010 at 02:59 AM.

  6. #6
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Yes, but I am using numbers, not names. If 06 goes in as the month, 5 days are added, shouldn't 06 come back out?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pahiker View Post
    Yes, but I am using numbers, not names. If 06 goes in as the month, 5 days are added, shouldn't 06 come back out?
    It doesn't matter since you are parsing those numbers into Date objects which are used to create Calendar objects, so they are no longer numbers, they are Dates or Calendar objects. When you use Calendar.get(...) this matters.

    Again, use the formatter Luke, use the formatter.

  8. #8
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Thanks, Yogurt.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Sorry for using improper grammar, am I. Me re-post my comments above let. Yes, hmmm.

    It matters not since you are parsing those numbers into date objects which are used to create calendar objects, so no longer numbers, they are, dates or calendar objects, they are. When you use calendar.get(...) this matters.

Similar Threads

  1. julian date to full date format
    By judy318 in forum New To Java
    Replies: 7
    Last Post: 11-02-2009, 01:17 PM
  2. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 09:10 AM
  3. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 09:32 PM
  4. Difference between current date and anothe date
    By vijay balusamy in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 05:15 PM
  5. Date math
    By orchid in forum New To Java
    Replies: 2
    Last Post: 04-18-2007, 08:01 AM

Posting Permissions

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