Results 1 to 7 of 7
  1. #1
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default I need to add days to a Calendar (Datepickerdialog)... how do I do this?

    I need to add a few days to make an end date (for instance the start date is the first date selected, and then the end date is x amount of days after that... I think I should be using the add() method but I am not quite sure of where or how to do so)

    Please assist!

    Thank you


    Java Code:
        //Get an instance of Calendar and DateFormat class, and get current date from the system using OnClick method
        Calendar startVacation = Calendar.getInstance();
        DateFormat fmtDate = DateFormat.getDateInstance();
        DatePickerDialog.OnDateSetListener startVaca = new DatePickerDialog.OnDateSetListener() {
    		
    		@Override
    		public void onDateSet(DatePicker view, int year, int monthOfYear,
    				int dayOfMonth) {
    			// TODO Auto-generated method stub
    			startVacation.set(Calendar.YEAR, year);
    			startVacation.set(Calendar.MONTH, monthOfYear);
    			startVacation.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    			reservation.setText("You have reserved the " + cabinId + " starting on " + fmtDate.format(startVacation.getTime()) + " and ending on " + Xday);
    			
    		}

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: I need to add days to a Calendar (Datepickerdialog)... how do I do this?

    Quote Originally Posted by IanKW View Post
    I think I should be using the add() method but I am not quite sure of where or how to do so
    Read the API documentation of the Calendar class and you know for sure that you should use the add( ... ) method and how you should use it.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: I need to add days to a Calendar (Datepickerdialog)... how do I do this?

    Hey Jos, I have read the documentation already, but my trouble is what I try keeps throwing errors... for instance:

    Java Code:
    add(Calendar.DAY_OF_MONTH, 3)
    added here:

    //Get an instance of Calendar and DateFormat class, and get current date from the system using OnClick method
    Calendar startVacation = Calendar.getInstance();
    DateFormat fmtDate = DateFormat.getDateInstance();
    DatePickerDialog.OnDateSetListener startVaca = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
    int dayOfMonth) {
    // TODO Auto-generated method stub

    startVacation.set(Calendar.YEAR, year);
    startVacation.set(Calendar.MONTH, monthOfYear);
    startVacation.set(Calendar.DAY_OF_MONTH, dayOfMonth);

    reservation.setText("You have reserved the " + cabinId + " starting on " + fmtDate.format(startVacation.getTime()) + " and ending on "+ fmtDate.format(startVacation.add(Calendar.DAY_OF_M ONTH, 3)));


    }


    };

    throws this:

    The method format(Date) in the type DateFormat is not applicable for the arguments (void)
    Im so frustrated, I have been trying at this all night before I asked and have searched and searched and read countless threads and in the documentation but nothing has helped, any chance you could give me an example based off of my code or just show me where I am going wrong? I would seriously appreciate it!
    Last edited by IanKW; 10-25-2014 at 06:44 AM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: I need to add days to a Calendar (Datepickerdialog)... how do I do this?

    It doesn't 'throw' anything, it's the compiler that tells you something's wrong in your code: the format( ... ) method needs a Date value for a parameter (as the compiler is trying to tell you), but you're supplying it the return value of the add( ... ) method which is void (which can be found in the API documentation). The two types don't match and the compiler starts nagging about it.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: I need to add days to a Calendar (Datepickerdialog)... how do I do this?

    can you show me what part of the documentation you are referring to? I am looking here:

    Calendar | Android Developers

    ...

    I was able to add this:

    startVacation.add(Calendar.DATE, 3);
    reservation.setText("You have reserved the " + cabinId + " starting on " + fmtDate.format(startVacation.getTime()) + " and ending on " + fmtDate.format(startVacation.getTime()));

    and it will add 3 days to the date, but it displays the same date for each of the elements starts on x date and ends on x date..

    how do I make them equal to the correct values?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: I need to add days to a Calendar (Datepickerdialog)... how do I do this?

    What happens when you add to the Calendar.DAY_OF_YEAR field? When I do this:

    Java Code:
    		Calendar c= Calendar.getInstance(TimeZone.getDefault());
    		System.out.println(c.getTime());
    		c.add(Calendar.DAY_OF_YEAR, 3);
    		System.out.println(c.getTime());
    ... it gives me the correct result ...

    kind regards.

    Jos

    ps. and swap line #1 and line #2 in your code.
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: I need to add days to a Calendar (Datepickerdialog)... how do I do this?

    It appears it just was not possible from within one textview, hence making two of them produced my desired result. Thanks

Similar Threads

  1. number of the days (-) days gone past
    By sh4rif in forum New To Java
    Replies: 4
    Last Post: 05-22-2012, 01:17 PM
  2. Calendar now = Calendar.getInstance()
    By volkvanmyn25 in forum New To Java
    Replies: 3
    Last Post: 10-31-2011, 10:25 PM
  3. Replies: 4
    Last Post: 09-21-2011, 04:39 PM
  4. Rolling forward days in a calendar
    By kumalh in forum New To Java
    Replies: 16
    Last Post: 08-04-2011, 07:03 PM
  5. Calendar days mixed up..
    By tomblue84 in forum AWT / Swing
    Replies: 0
    Last Post: 03-17-2010, 05:08 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
  •