Bookmark Topic Watch Topic
  • New Topic

comparison on YEAR attribute of Calendar  RSS feed

 
john ruane
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
i am using a piece of java script to get a GUI display for a user to select a date for an application i am working on.
what i then want to do is to test whether the date selected is plus or minus one year of the current date.

the users date is saved in Date format, but i am unable to isolate the YEAR attribute when converting it to a Calendar object. this is what iv tried so far:


deliveryDate = dateFormat.parse(deliveryDateStr); // the user inputted date

Calendar currentCalendar = new GregorianCalendar();

Calendar userCalendar = new GregorianCalendar();
userCalendar.setTime(deliveryDate);


if (userCalendar.get(Calendar.YEAR) > currentCalendar.get(Calendar.YEAR+1) || userCalendar.get(Calendar.YEAR) < currentCalendar.get(Calendar.YEAR-1)) {
throw new ParseException("Year out of bounds.", 1);
}

any ideas?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Welcome to JavaRanch

if (userCalendar.get(Calendar.YEAR) > currentCalendar.get(Calendar.YEAR+1) || userCalendar.get(Calendar.YEAR) < currentCalendar.get(Calendar.YEAR-1))


How about this:

 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not post the same question more than once. It creates confusion and duplication of effort as the community tries to help everyone.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!