Results 1 to 3 of 3
  1. #1
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Rep Power

    Default Add Date Problem

    Apologies in advance if this is in the wrong section, I wasn't really sure where to put it.

    I'm trying to add increment a date by 1. This is how I'm trying to do it:

    Java Code:
    Calendar now = Calendar.getInstance();
    Calendar thisweek = Calendar.getInstance();
    thisweek.set(Calendar.WEEK_OF_YEAR, now.get(Calendar.WEEK_OF_YEAR));
    thisweek.set(Calendar.YEAR, now.get(Calendar.YEAR));
    Date thisWeekDate = thisweek.getTime();
    Date tuesday = thisweek.add(Calendar.DATE, 1);
    Out of interest, I also tried it this way:
    Java Code:
    Calendar now = Calendar.getInstance();
    Calendar thisWeekCalendar = Calendar.getInstance();
    thisWeekCalendar.set(Calendar.WEEK_OF_YEAR, now.get(Calendar.WEEK_OF_YEAR));
    thisWeekCalendar.set(Calendar.YEAR, now.get(Calendar.YEAR));
    Date thisWeekDate = thisWeekCalendar.getTime();
    Calendar weekAdd = Calendar.getInstance();
    Date monday = weekAdd.getTime();
    Date tuesday = weekAdd.add(Calendar.DATE, 1);
    Both had the same outcome which was this error:
    Java Code:
    An error occurred at line: 15 in the jsp file: /enterStudentGradebook.jsp
    Type mismatch: cannot convert from void to Date
    12: thisweek.set(Calendar.YEAR, now.get(Calendar.YEAR));
    14: Date thisWeekDate = thisweek.getTime();
    15: Date tuesday = thisweek.add(Calendar.DATE, 1);
    17:  %>
    Any ideas? =\

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Richmond, Virginia
    Blog Entries
    Rep Power


    I moved it to new to Java for you since it seems to be a problem with the Date class, and nothing to do with Applets.

    The problem is being told to you.
    Java Code:
    thisweek.add(Calendar.DATE, 1); //should be thisWeek
    The add method returns void. It changes the object it's called on, you shouldn't be assigning it to anything. Try something like
    Java Code:
    thisweek.add(Calendar.DATE, 1);
    Date tuesday = thisweek;

  3. #3
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Rep Power


    Ah, that would certainly be a problem, thanks for that =)

    Edit: Worked like a charm, thanks a lot! =)
    Last edited by UnfairBear; 08-14-2011 at 03:17 PM.

Similar Threads

  1. Add Date Problem
    By UnfairBear in forum Java Servlet
    Replies: 0
    Last Post: 08-13-2011, 10:33 PM
  2. sql date problem
    By realosso in forum New To Java
    Replies: 3
    Last Post: 06-04-2010, 05:42 AM
  3. date problem
    By karlkwanny in forum JDBC
    Replies: 1
    Last Post: 07-30-2009, 11:50 AM
  4. problem with date class
    By gabriel in forum New To Java
    Replies: 3
    Last Post: 08-03-2007, 02:28 PM
  5. problem with date field
    By Ed in forum JDBC
    Replies: 2
    Last Post: 06-12-2007, 05:27 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