0

I have taken out irrelevant code for my question and left in what is relevant for my problem. It is stating that the Integer is too large. I have converted the current time to Milliseconds and I am trying to add a month, 2 weeks and 1 week in milliseconds to achieve the 'Date Expired'. If anyone knows how I would go about using Long instead of int? I'm confused as my Date isn't delclared as an Integer?

It is stating the error at 'dateExpired = dateExpired + 2628000000;'.

public class VIP implements Serializable {
    private Date dateExpired;

    public VIP(Date dateExpired) {
        this.dateExpired = new Date(Calendar.getInstance().getTimeInMillis());
    }


    public Date getDateExpired() {
        return dateExpired;
    }

    public void setDateExpired(String ticketType) {
       if (ticketType.equals("Gold")) {
            dateExpired =  dateExpired + 2628000000;
        } else if (ticketType.equals("Silver")) {
            dateExpired = dateExpired + 1209600000;
        } else {
            dateExpired = dateExpired + 604800000;
        }
    }
}
user3597639
  • 47
  • 2
  • 11

2 Answers2

9

Add the token L after your numbers to turn them into long literals:

public void setDateExpired(String ticketType) {
   if (ticketType.equals("Gold")) {
        dateExpired = dateExpired + 2628000000L;
    } else if (ticketType.equals("Silver")) {
        dateExpired = dateExpired + 1209600000L;
    } else {
        dateExpired = dateExpired + 604800000L;
    }
}

Without they L, the compiler interprets them as int literals, but the values are too big for the int type. The largest int value you can use is 2147483647 (though it's best not to mix types in cases like this, and just use longs throughout).

You'll also run into the issue of adding incorrect types. Either change dateExpired to be a long (and convert it to a Date later), or use this form:

dateExpired = new Date(dateExpired.getTime() + 604800000L) 

Just an aside, it might help you later to document what the big numbers mean:

dateExpired = dateExpired + 86400000L; // one day
mk.
  • 11,360
  • 6
  • 40
  • 54
  • Hi thank you for your reply. Its now stating ' bad operand types for binary operator '+' . First type Date , Second type Long'. – user3597639 Feb 23 '15 at 16:05
  • I should be commenting more is right. Will save confusion later on i guess. Thanks. – user3597639 Feb 23 '15 at 16:09
  • Yep, now the issue is that you're adding a Date to a long. Just pick whether you want to use Date or long to represent your value (I'd go with `long`, myself), and change your code accordingly (I've added that part to my answer) – mk. Feb 23 '15 at 16:11
1

You cannot add Date object to Long Object

use

public void setDateExpired(String ticketType) {
   if (ticketType.equals("Gold")) {
        dateExpired =  new Date(dateExpired.getTime() + 2628000000L);
    } else if (ticketType.equals("Silver")) {
        dateExpired = new Date(dateExpired.getTime() + 1209600000L);
    } else {
        dateExpired = new Date(dateExpired.getTime() + 604800000L);
    }
}
Matteo Rubini
  • 831
  • 5
  • 9