I am trying to write some code to correctly set an expiration date given a certain date.
For instance this is what i have.
Date lastSignupDate = m.getLastSignupDate(); long expirationDate = 0; long milliseconds_in_half_year = 15778463000L; expirationDate = lastSignupDate.getTime() + milliseconds_in_half_year; Date newDate = new Date(expirationDate);
However, say if i the sign up date is on 5/7/2011 the expiration date output i get is on 11/6/2011 which is not exactly half of a year from the given date. Is there an easier way to do this?
Advertisement
Answer
I would use the Calendar class – the add method will do this kind of thing perfectly.
http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html
Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, 6); java.util.Date expirationDate = cal.getTime(); System.err.println(expirationDate);