How to set expiration date in Java

I'm trying to write some code to correctly set the expiration date of a given date

For example, 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, if my registration date is May 7, 2011, I get the expiration date output on 11 / 6 / 2011, which is not half of the given date Is there a simpler way?

Solution

I'll use the calendar class - add method to do this 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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>