Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting a cookie to expire at midnight

 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a servlet that is supposed to set a cookie to expire at midnight on the day it was set. Not having used cookies in Java before (or time, for that matter), I am having trouble. Below is my code:
Cookie votingCookie = new Cookie ("voting", "voted");
Calendar now= new GregorianCalendar();
Calendar tonight= new GregorianCalendar();
Date time= new Date();
now.setTime(time);
tonight.set(now.YEAR, now.MONTH, now.DATE, 24,00);
long nowSeconds= (now.HOUR_OF_DAY * 3600) + (now.MINUTE * 60);
long tonightSeconds= (tonight.HOUR_OF_DAY * 3600) + (tonight.MINUTE * 60);
System.out.println("Tonight Hour of Day= " + tonight.HOUR_OF_DAY);
System.out.println("Tonight Minute= " + tonight.MINUTE);
System.out.println("Now Hour of Day= " + now.HOUR_OF_DAY);
System.out.println("Now Minute= " + now.MINUTE);
System.out.println("Tonight Seconds= " + tonightSeconds);
System.out.println("Now Seconds= " + nowSeconds);
System.out.println("Num seconds= " + (tonightSeconds - nowSeconds) );
votingCookie.setMaxAge((int)(tonightSeconds - nowSeconds));
response.addCookie(votingCookie);
------------

The standard output in the Jrun log is:
Tonight Hour of Day= 11
Tonight Minute= 12
Now Hour of Day= 11
Now Minute= 12
Tonight Seconds= 40320
Now Seconds= 40320
Num seconds= 0
------
In fact, I get an hour of 11 and a minute of 12 no matter when I run the servlet. Anyway, I'm stumped, and any help is greatly appreciated.
 
Paul Wetzel
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have this figured out...The public static final int's that you use (such as tonight.HOUR_OF_DAY) are ints. They are used to get info from a GregorianCalendar object (gregCal.get(GregorianCalendar.HOUR_OF_DAY). whenever you see something like that in all capital letters you can pretty much count that it is a constant (or public static final). I think that the calls should look like this
hope this is correct
paul
[This message has been edited by Paul Wetzel (edited November 17, 2000).]
 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks! That solved it. I should have caught that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic